-
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 sole to the query builder #35869
Conversation
*/ | ||
public function sole($columns = ['*']) | ||
{ | ||
$result = $this->get($columns); |
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.
Shouldn't we always force it to take(2)
instead of relying on the user's criteria (which could potentially load many models in memory)?
Also, although the docblocks state the exceptions, shouldn't we adhere to the *OrFail
convention?
Not trying to nitpick here, just thought it would make an interesting discussion. ;)
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.
Good call on take()
added that. As for naming, I leave that to Taylor :)
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.
Isn't *OrFail
needed only if there's a silent version of the method ? (As rails add a bang "!" version of methods only if there is a non bang version ?)
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.
Yes, although in this case the "silent" version of sole
would be the same as the first()
, so we wouldn't need its silent version, I think.
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.
Ah yes, didn't see it that way. And it doesn't hurt to be explicit anyway, especially since it's a convention that already exists in the framework.
Either way, I'm happy with the result!
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.
❤️
* Execute the query and get the first result if it's the sole. | ||
* | ||
* @param array|string $columns | ||
* @return \Illuminate\Database\Eloquent\Model|object|static|null |
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.
This simplifies to object|null
.
I wonder if we should use |
Similar to Django's
get()
https://docs.djangoproject.com/en/3.1/topics/db/queries/#retrieving-a-single-object-with-get and Rails'.sole
andfind_sole_by
https://github.com/rails/rails/blob/master/activerecord/CHANGELOG.md.DB::table('products')->where('ref', '#123')->sole()
will return the only record that matches the criteria, if no records found aNoRecordsFoundException
will be thrown, and if multiple records were found aMultipleRecordsFoundException
will be thrown.