Skip to content

feat: add firstOrFail and findOrFail, bind repository model type via generics - #319

Open
vitgrams wants to merge 5 commits into
masterfrom
vitgrams/firstOrNull
Open

vitgrams wants to merge 5 commits into
masterfrom
vitgrams/firstOrNull

Conversation

@vitgrams

Copy link
Copy Markdown
Contributor

Why.
PHPStan in consumer projects reported errors on repository results: every finder returned a nullable value, and the result was typed as the base Model, so accessing the concrete model's properties was flagged too.

What changed.

  • Added fail-fast finders to EntityControlTrait: firstOrFail($where = []) and findOrFail($id). When nothing is found they throw ModelNotFoundException with the repository's model class, which Laravel renders as 404.
  • Added the PHPDoc template @template TModel of Model = Model to EntityControlTrait and BaseRepository. A consumer binds its model with /** @extends BaseRepository<User> */.
  • first, find, findBy, last and update do not change at runtime and still return null when nothing is found. Native signatures are untouched; updateOrCreate only gets @return TModel in PHPDoc.
  • Tests and documentation for the new methods and the model binding.

Verification.

  • vendor/bin/phpunit: OK (500 tests, 1874 assertions).
  • vendor/bin/pint --test: PASS, 128 files.
  • One-off run of Larastan 3.15 / PHPStan 2.3.7 at level max (not added to dependencies): on the two changed src files, 126 errors on master vs 81 on this branch, and the annotations add no new errors.
  • Consumer fixture: a bound repository resolves to TestModel for the fail-fast finders, TestModel|null for the nullable finders and Collection<int, TestModel> for collections; an unbound repository resolves to Model.

Notes.

  • A consumer repository that already declares its own firstOrFail / findOrFail with an incompatible signature will get a fatal error when the class is loaded.
  • The template default (= Model) requires PHPStan 1.12+ or 2.x.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-19T15:58:57.120901Z fd77bce PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@vitgrams vitgrams self-assigned this Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant