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

[9.x] Add soleValue method to query builders #41368

Merged
merged 1 commit into from
Mar 7, 2022
Merged

Conversation

hailwood
Copy link
Contributor

@hailwood hailwood commented Mar 7, 2022

Currently we have the following methods

$query->sole(['id']); // bails if there is not exactly one result, but returns an object
$query->value('id');  // returns just the value, but no safety around there being exactly one result

However if we want the id of the sole result we must do $query->sole(['id'])->id.

This PR adds a new method to both builders soleValue which is the same logic as value (removing the null checks since sole will bail) and uses sole instead of first internally allowing us to do

$query->sole(['id']);    // bails if there is not exactly one result, but returns an object
$query->value('id');     // returns just the value, but no safety around there being exactly one result
$query->soleValue('id'); // bails if there is not exactly one result and returns just the value

@GrahamCampbell GrahamCampbell changed the title Add soleValue method to query builders [9.x] Add soleValue method to query builders Mar 7, 2022
@taylorotwell taylorotwell merged commit bf0ebd6 into laravel:9.x Mar 7, 2022
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.

2 participants