-
Notifications
You must be signed in to change notification settings - Fork 11.3k
[5.1] Add missing *orFail methods in relationships #9143
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
Conversation
I'll be honest I have a little bit of a hard time understanding the value of these methods. Why not just use the related model directly? |
Hmm that's true. However why does |
Not sure. To me it makes more sense to just go straight to the model you want. No point in going through the relationship. |
I agree with you. Should we deprecate the two |
@taylorotwell I disagree. Going through the relationship methods help a ton when creating / finding records for For an example, I can make sure that a
If I was using the related model directly, I'd need to query through the pivot table manually, or by using a Going through a relationship is also great because you have a single point where your foreign key is declared in your relationship, and if I ever need to change it, I literally just change the string in the Querying relations is advertised in the documentation for versions 4.2, 5.0, 5.1, and Master: http://laravel.com/docs/master/eloquent-relationships#querying-relations I really think these methods should be added to keep consistency with the query builder API. I'd really like to use Please let me know your thoughts, thanks! |
@lukasgeiter @taylorotwell This one should be merged. Lack of It's helpful to have the relation constraint applied automatically when calling |
I still don't understand why even go through the relationship at all if you On Saturday, July 18, 2015, Jarek Tkaczyk [email protected] wrote:
|
@taylorotwell Because you may want to check whether, say, a user owns a resource:
rather than
|
Also, without going through the relationship, we'd need to retrieve extra pivot data manually, would we not? |
I've been using this the same way @jarektkaczyk mentioned, if there's a more appropriate alternative way to do this I'm happy to change my ways, but this seems to be a pretty quick and convenient way to ensure ownership on related models. |
Any update on this? |
This needs rebasing. |
Done. |
$result = $this->find($id, $columns); | ||
|
||
if (is_array($id)) { | ||
if (count($result) == count(array_unique($id))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
===
please
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unless our other code uses ==
for some strange reason
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well the Eloquent Builder uses ==
OK I'm confused again. I already use |
[5.1] Add missing *orFail methods in relationships
Nevermind, read original bug description. |
…ptions The regression in laravel#7048 and laravel#19019 is also observed in laravel#9143, because the `find`, `findMany` and `findOrFail` methods are copied from the Eloquent Builder to the `BelongsToMany` and `HasManyThrough` relations. For this reason, we need to convert the passed ids to an array before executing the queries.
The regression in laravel#7048 and laravel#19019 is also observed in laravel#9143, because the `find`, `findMany` and `findOrFail` methods are copied from the Eloquent Builder to the `BelongsToMany` and `HasManyThrough` relations. For this reason, we need to convert the passed ids to an array before executing the queries.
The regression in laravel#7048 and laravel#19019 is also observed in laravel#9143, because the `find`, `findMany` and `findOrFail` methods are copied from the Eloquent Builder to the `BelongsToMany` and `HasManyThrough` relations. For this reason, we need to convert the passed ids to an array before executing the queries.
…regression in laravel#7048 and laravel#19019 is also observed in laravel#9143, because the `find`, `findMany` and `findOrFail` methods are copied from the Eloquent Builder to the `BelongsToMany` and `HasManyThrough` relations.For this reason, we need to convert the passed ids to an array before executing the queries.
* Ensure Builder::findOrFail with Arrayable throws ModelNotFoundException I found a regression in #7048 and #19019, where the Eloquent Builder will not throw a `ModelNotFoundException` when an `Arrayable` is passed to `findOrFail`. In this #7048, we are checking if the `$ids` is an array and we count the results against the number of ids. But since #19019, the `find` method also accepts an `Arrayable` for the ids. So if an `Arrayable` is passed, the check is skipped and the method returns the results. To fix this, we are first checking if the `$ids` is an `Arrayable` and we convert the ids to an array before checking the results. * Ensure find* methods on relationships are accepting Arrayable idsThe regression in #7048 and #19019 is also observed in #9143, because the `find`, `findMany` and `findOrFail` methods are copied from the Eloquent Builder to the `BelongsToMany` and `HasManyThrough` relations.For this reason, we need to convert the passed ids to an array before executing the queries.
Fixes #9138
This adds
findOrFail
toBelongsToMany
andfindOrFail
/firstOrFail
toHasManyThrough