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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,20 @@ public function value($column)
}
}

/**
* Get a single column's value from the first result of a query if it's the sole matching record.
*
* @param string|\Illuminate\Database\Query\Expression $column
* @return mixed
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model>
* @throws \Illuminate\Database\MultipleRecordsFoundException
*/
public function soleValue($column)
{
return $this->sole([$column])->{Str::afterLast($column, '.')};
}

/**
* Get a single column's value from the first result of the query or throw an exception.
*
Expand Down
16 changes: 16 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2516,6 +2516,22 @@ public function value($column)
return count($result) > 0 ? reset($result) : null;
}

/**
* Get a single column's value from the first result of a query if it's the sole matching record.
*
* @param string $column
* @return mixed
*
* @throws \Illuminate\Database\RecordsNotFoundException
* @throws \Illuminate\Database\MultipleRecordsFoundException
*/
public function soleValue($column)
{
$result = (array) $this->sole([$column]);

return reset($result);
}

/**
* Execute the query as a "select" statement.
*
Expand Down