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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/Illuminate/Database/Concerns/BuildsQueries.php
Expand Up @@ -3,6 +3,8 @@
namespace Illuminate\Database\Concerns;

use Illuminate\Container\Container;
use Illuminate\Database\MultipleRecordsFoundException;
use Illuminate\Database\NoRecordsFoundException;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;

Expand Down Expand Up @@ -147,6 +149,30 @@ public function first($columns = ['*'])
return $this->take(1)->get($columns)->first();
}

/**
* 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.

*
* @throws \Illuminate\Database\NoRecordsFoundException
* @throws \Illuminate\Database\MultipleRecordsFoundException
*/
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!


if ($result->isEmpty()) {
throw new NoRecordsFoundException();
}

if ($result->count() > 1) {
throw new MultipleRecordsFoundException();
}

return $result->first();
}

/**
* Apply the callback's query changes if the given "value" is true.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Database/MultipleRecordsFoundException.php
@@ -0,0 +1,10 @@
<?php

namespace Illuminate\Database;

use RuntimeException;

class MultipleRecordsFoundException extends RuntimeException
{
//
}
10 changes: 10 additions & 0 deletions src/Illuminate/Database/NoRecordsFoundException.php
@@ -0,0 +1,10 @@
<?php

namespace Illuminate\Database;

use RuntimeException;

class NoRecordsFoundException extends RuntimeException
{
//
}
27 changes: 27 additions & 0 deletions tests/Integration/Database/QueryBuilderTest.php
Expand Up @@ -3,6 +3,8 @@
namespace Illuminate\Tests\Integration\Database;

use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\MultipleRecordsFoundException;
use Illuminate\Database\NoRecordsFoundException;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
Expand Down Expand Up @@ -30,6 +32,31 @@ protected function setUp(): void
]);
}

public function testSole()
{
$expected = ['id' => '1', 'title' => 'Foo Post'];

$this->assertEquals(1, DB::table('posts')->where('title', 'Foo Post')->sole()->id);
}

public function testSoleFailsForMultipleRecords()
{
DB::table('posts')->insert([
['title' => 'Foo Post', 'content' => 'Lorem Ipsum.', 'created_at' => new Carbon('2017-11-12 13:14:15')],
]);

$this->expectException(MultipleRecordsFoundException::class);

DB::table('posts')->where('title', 'Foo Post')->sole();
}

public function testSoleFailsIfNoRecords()
{
$this->expectException(NoRecordsFoundException::class);

DB::table('posts')->where('title', 'Baz Post')->sole();
}

public function testSelect()
{
$expected = ['id' => '1', 'title' => 'Foo Post'];
Expand Down