Skip to content

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

Merged
taylorotwell merged 1 commit into
laravel:9.xfrom
hailwood:9.x
Mar 7, 2022
Merged

[9.x] Add soleValue method to query builders#41368
taylorotwell merged 1 commit into
laravel:9.xfrom
hailwood:9.x

Conversation

@hailwood
Copy link
Copy Markdown
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