Skip to content
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
26 changes: 26 additions & 0 deletions src/Illuminate/Database/Concerns/BuildsQueries.php
Original file line number Diff line number Diff line change
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->take(2)->get($columns);

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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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