Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[12.x] allow for custom builders in QueriesRelationships closures#55362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
zjbarg wants to merge1 commit intolaravel:12.x
base:12.x
Choose a base branch
Loading
fromzjbarg:allow-custom-builders-on-relationship-queries

Conversation

zjbarg
Copy link

@zjbargzjbarg commentedApr 10, 2025
edited
Loading

The closure types added in#54452 are not accounting for custom query builders, and therefore the following code is triggering aargument.type error because the closure expectsBuilder<Order>

Parameter #2 $callback of method Illuminate\Database\Eloquent\Builder<OrderItem>::whereHas()  expects (Closure(Illuminate\Database\Eloquent\Builder<Order>): mixed)|null,                         Closure(OrderBuilder): OrderBuilder given.
OrderItem::query()->whereHas('order',fn (OrderBuilder$q) =>$q->shipped());

@zjbargzjbarg marked this pull request as draftApril 10, 2025 21:35
@zjbargzjbarg changed the titlefeat: allow for custom builders in QueriesRelationships closures[12.x] allow for custom builders in QueriesRelationships closuresApr 10, 2025
@calebdw
Copy link
Contributor

calebdw commentedApr 11, 2025
edited
Loading

@zjbarg,

Thanks for attempting this---however, what you are trying to do is not possible with PHPDocs alone.

Given the following example:

    /**     * Add a relationship count / exists condition to the query with where clauses.     *     * @template TRelatedModel of \Illuminate\Database\Eloquent\Model+    * @template TRelatedModelBuilder of \Illuminate\Database\Eloquent\Builder<TRelatedModel>     *     * @param  \Illuminate\Database\Eloquent\Relations\Relation<TRelatedModel, *, *>|string  $relation-    * @param  (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>): mixed)|null  $callback+    * @param  (\Closure(TRelatedModelBuilder): mixed)|null  $callback     * @param  string  $operator     * @param  int  $count     * @return $this     */    public function whereHas($relation, ?Closure $callback = null, $operator = '>=', $count = 1)

method templates can only be usedto extract type information from the given parameters. Meaning that:

  • TRelatedModelBuilder cannot work because there's no builder type being passed to the method
  • phpstan is unable to resolve the template type which is why it'smixed
  • at best, the type (even if phpstan could resolve it) would be\Illuminate\Database\Eloquent\Builder<TRelatedModel> which is what is already provided

Also, the existing template type (TRelatedModel) can only be used with actual relation objects that are passed to the method (e.g.,$model->order()) because it's extracting the inner model type from the relation). It cannot work with strings ('order') because phpstan has no idea what the actual relation type is through PHPDocs alone (this is possible using custom PHPStan extensions, more on this later). See example from my linked PR:

$query->whereHas($user->posts(),function ($query) {// Knows that it's a Post Builder!assertType('Illuminate\Database\Eloquent\Builder<Illuminate\Types\Builder\Post>',$query);});

When using mypublished Larastan fork, Larastanis able to correctly determine what the builder type is and it handles custom builders (including nested relations). Here's some examples from the test suite:

User::query()->whereHas('accounts',function (Builder$query) {assertType('Illuminate\Database\Eloquent\Builder<App\Account>',$query);});User::query()->withWhereHas('accounts.posts',function (Builder|Relation$query) {assertType('App\PostBuilder<App\Post>|Illuminate\Database\Eloquent\Relations\BelongsToMany<App\Post, App\Account, Illuminate\Database\Eloquent\Relations\Pivot,\'pivot\'>',$query);});

Iupstreamed a PR with this functionality, however, the PR was closed because there's still somelimitations with PHPStan...I guess it's a matter of if you want no support at all until it's fully supported or mostly supported with somequirks.


In the meantime, if you want your editor and phpstan to know the builder type (without using my fork), you cannarrow the type:

OrderItem::query()->whereHas('order',function ($q) {assert($qinstanceof OrderBuilder);return$q->shipped();});

Best,

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@zjbarg@calebdw

[8]ページ先頭

©2009-2025 Movatter.jp