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

[11.x] Introduce firstOrFail for Database Query Builder #51381

Closed
wants to merge 9 commits into from
18 changes: 18 additions & 0 deletions src/Illuminate/Database/Concerns/BuildsQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\MultipleRecordsFoundException;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\RecordNotFoundException;
use Illuminate\Database\RecordsNotFoundException;
use Illuminate\Pagination\Cursor;
use Illuminate\Pagination\CursorPaginator;
Expand Down Expand Up @@ -335,6 +336,23 @@ public function first($columns = ['*'])
return $this->take(1)->get($columns)->first();
}

/**
* Execute the query and get the first result or throw an exception.
*
* @param array|string $columns
* @return \Illuminate\Database\Eloquent\Model|object|static
*
* @throws \Illuminate\Database\RecordNotFoundException
*/
public function firstOrFail($columns = ['*'], $message = null)
{
if (! is_null($result = $this->first($columns))) {
return $result;
}

throw new RecordNotFoundException($message ?: 'No record found for the given query.');
}

/**
* Execute the query and get the first result if it's the sole matching record.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Database/RecordNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Illuminate\Database;

use RuntimeException;

class RecordNotFoundException extends RuntimeException
{
//
}
25 changes: 25 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Illuminate\Database\Query\Processors\MySqlProcessor;
use Illuminate\Database\Query\Processors\PostgresProcessor;
use Illuminate\Database\Query\Processors\Processor;
use Illuminate\Database\RecordNotFoundException;
use Illuminate\Pagination\AbstractPaginator as Paginator;
use Illuminate\Pagination\Cursor;
use Illuminate\Pagination\CursorPaginator;
Expand Down Expand Up @@ -2758,6 +2759,30 @@ public function testFirstMethodReturnsFirstResult()
$this->assertEquals(['foo' => 'bar'], $results);
}

public function testFirstOrFailMethodReturnsFirstResult()
{
$builder = $this->getBuilder();
$builder->getConnection()->shouldReceive('select')->once()->with('select * from "users" where "id" = ? limit 1', [1], true)->andReturn([['foo' => 'bar']]);
$builder->getProcessor()->shouldReceive('processSelect')->once()->with($builder, [['foo' => 'bar']])->andReturnUsing(function ($query, $results) {
return $results;
});
$results = $builder->from('users')->where('id', '=', 1)->firstOrFail();
$this->assertEquals(['foo' => 'bar'], $results);
}

public function testFirstOrFailMethodThrowsRecordNotFoundException()
{
$builder = $this->getBuilder();
$builder->getConnection()->shouldReceive('select')->once()->with('select * from "users" where "id" = ? limit 1', [1], true)->andReturn([]);

$builder->getProcessor()->shouldReceive('processSelect')->once()->with($builder, [])->andReturn([]);

$this->expectException(RecordNotFoundException::class);
$this->expectExceptionMessage('No record found for the given query.');

$builder->from('users')->where('id', '=', 1)->firstOrFail();
}

public function testPluckMethodGetsCollectionOfColumnValues()
{
$builder = $this->getBuilder();
Expand Down