Skip to content
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

Merged
merged 3 commits into from Jan 14, 2021
Merged

[8.x] Add sole to the query builder #35869

merged 3 commits into from Jan 14, 2021

Conversation

themsaid
Copy link
Member

@themsaid themsaid commented Jan 13, 2021

Similar to Django's get() https://docs.djangoproject.com/en/3.1/topics/db/queries/#retrieving-a-single-object-with-get and Rails' .sole and find_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 a NoRecordsFoundException will be thrown, and if multiple records were found a MultipleRecordsFoundException will be thrown.

*/
public function sole($columns = ['*'])
{
$result = $this->get($columns);
Copy link
Contributor

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. ;)

Copy link
Member Author

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 :)

Copy link

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 ?)

Copy link
Contributor

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.

Copy link

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!

Copy link

@the-dijkstra the-dijkstra left a 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
Copy link
Member

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.

@taylorotwell
Copy link
Member

I wonder if we should use ModelNotFoundException if there are no results so it's the same exception type as firstOrFail and findOrFail? Of course, this would only apply to Eloquent queries.

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.

None yet

6 participants