-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[8.x] Add whereBelongsTo()
Eloquent builder method
#38927
[8.x] Add whereBelongsTo()
Eloquent builder method
#38927
Conversation
There is already a method for this purpose The # magic method
->whereCountry("USA")
# will become
->where("country", "USA") so someone might be using it like If this PR gets merged, people will be sending similar PRs for all of other relations. I would suggest not to introduce more alias methods in query builder, as this increase the learning curve. |
Thanks for the feedback, but I disagree on all points.
Please read through my description. I don't think
The possibility of someone using
Not really, most of the relationship types are not suitable for this sort of method. That only applies to relationship types whereby the foreign key is on the related model. So - only
This is not an increase to the "learning curve". That only really happens when extra features are introduced that you must use, or changes that make existing features too complex. This is an optional helper method that improves code clarity and maintainability in many places throughout an app. |
Can you make this work with |
Please read my description. I don't know how passing the class name here is useful. |
Ah I think I see it now. So in which situations are you bound to using |
For example, this is a pretty common case when you're inside a multitenant application. I'll give you a couple examples: Listing an author's posts inside a particular category: $author->posts()->whereBelongsTo($category)->get() Filtering a list of songs by album and artist: Song::query()->whereBelongsTo($album)->whereBelongsTo($artist)->get() |
Kinda feels like some of these checks should throw exceptions instead of just returning |
All done. |
I think you should use framework/src/Illuminate/Database/Eloquent/Builder.php Lines 716 to 720 in 4a4c1de
|
Done that too. I've also added an extra property to |
@danharrin |
* Add failing test for whereBelongsTo * Implement whereBelongsTo * mock in tests * Add test for relationship guess * Add exceptions * Remove early returns * Update Builder.php * Update Builder.php * Throw RelationNotFoundException * Refactor to catch BadMethodCallException * Remove rogue get_class * move method to trait * qualify foreign key * formatting Co-authored-by: Taylor Otwell <[email protected]>
Hey 👋
When retrieving
belongsTo
related records, you can usually use the inverse (hasMany
) of the relationship, like so:However, sometimes this is not possible, and you must filter an existing query by its parent record:
This works, but explicitly depends on the name of foreign key (
author_id
), and the name of the owner key (id
) in most cases. This is a maintenance burden, as these key names can be changed inside the relationship method and will subsequently become outdated across your entire app.This PR introduces a
whereBelongsTo()
method to the Eloquent builder. You may use it like so:It will automatically retrieve the foreign key name from the relationship (
author
), and the correct owner key from the related model ($author
).By default, the name of the relationship is guessed based on the class of the related model. Sometimes, this is not appropriate. If the
$author
is an instance ofApp\Models\User
,whereBelongsTo()
will search for auser()
relationship that does not exist. In this case, you may manually specify the relationship name: