Skip to content

[11.x] Add find sole query builder method#54667

Merged
taylorotwell merged 1 commit into
laravel:11.xfrom
zepfietje:add-find-sole-query-builder-method
Feb 18, 2025
Merged

[11.x] Add find sole query builder method#54667
taylorotwell merged 1 commit into
laravel:11.xfrom
zepfietje:add-find-sole-query-builder-method

Conversation

@zepfietje
Copy link
Copy Markdown
Contributor

@zepfietje zepfietje commented Feb 18, 2025

I find myself writing Model::whereKey($id)->sole(); a lot in my applications to retrieve a model that I know should exist. The findOrFail() method provides similar functionality, but there's no guarantee that only a single record exists in the database since it calls first() internally.

This pull request introduces a new findSole() method to retrieve a single model by its key, or throw an exception when no or multiple models match the query. Alternatively, findOrFail() could be updated to behave like this but that would be a breaking change. Personally, I also think findSole() reads nicer when what I want to do is "find a sole model which I know the key of".

If you want me to make any changes, clarify anything, or add specific tests, please let me know. 😊

@zepfietje zepfietje changed the title Add find sole query builder method [11.x] Add find sole query builder method Feb 18, 2025
@simonhamp
Copy link
Copy Markdown
Contributor

sole() already throws an exception if there's more than one result.

@zepfietje
Copy link
Copy Markdown
Contributor Author

sole() already throws an exception if there's more than one result.

Yeah I know, that's why findSole() uses that under the hood.
Or am I missing something? 🙂

@simonhamp
Copy link
Copy Markdown
Contributor

simonhamp commented Feb 18, 2025

Sorry, I interpreted your description as suggesting that sole doesn't throw an exception.

If you're getting a row by its ID, then by definition, there is already only one... So it's hard to see why findOrFail is insufficient?

I think you may need to provide a clearer example of your specific use-case.

@zepfietje
Copy link
Copy Markdown
Contributor Author

I'm aware that technically this shouldn't make a difference when your primary key column is unique on database level (as it should be).

However, I do like the explicit assertion that only a single record is returned instead of silently discarding any potential other records (though theoretically not needed when the primary key is unique).

I always use sole() instead of firstOrFail() for queries where I know there should only be a single record returned. In line with that, whereKey()->sole() and thus findSole() feel natural to me.

@taylorotwell taylorotwell merged commit ee0f81f into laravel:11.x Feb 18, 2025
@zepfietje zepfietje deleted the add-find-sole-query-builder-method branch February 18, 2025 15:27
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.

3 participants