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

[7.x] Added firstWhereOrFail to Eloquent Builder #31134

Closed
wants to merge 5 commits into from
Closed
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 @@ -249,6 +249,20 @@ public function firstWhere($column, $operator = null, $value = null, $boolean =
return $this->where($column, $operator, $value, $boolean)->first();
}

/**
* Add a basic where clause to the query, and return the first result or throw an exception.
*
* @param \Closure|string|array $column
* @param mixed $operator
* @param mixed $value
* @param string $boolean
* @return \Illuminate\Database\Eloquent\Model|static
*/
public function firstWhereOrFail($column, $operator = null, $value = null, $boolean = 'and')
{
return $this->where($column, $operator, $value, $boolean)->firstOrFail();
}

/**
* Add an "or where" clause to the query.
*
Expand Down
34 changes: 34 additions & 0 deletions tests/Integration/Database/EloquentWhereTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Tests\Integration\Database;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

Expand Down Expand Up @@ -91,6 +92,39 @@ public function testFirstWhere()
UserWhereTest::firstWhere(['name' => 'wrong-name', 'email' => 'test-email1'], null, null, 'or'))
);
}

public function testFirstWhereOrFail()
{
/** @var UserWhereTest $firstUser */
$firstUser = UserWhereTest::create([
'name' => 'test-name',
'email' => 'test-email',
'address' => 'test-address',
]);

/** @var UserWhereTest $secondUser */
$secondUser = UserWhereTest::create([
'name' => 'test-name1',
'email' => 'test-email1',
'address' => 'test-address1',
]);

$this->assertTrue($firstUser->is(UserWhereTest::firstWhereOrFail('name', '=', $firstUser->name)));
$this->assertTrue($firstUser->is(UserWhereTest::firstWhereOrFail('name', $firstUser->name)));
$this->assertTrue($firstUser->is(UserWhereTest::where('name', $firstUser->name)->firstWhereOrFail('email', $firstUser->email)));

$this->expectException(ModelNotFoundException::class);
UserWhereTest::where('name', $firstUser->name)->firstWhereOrFail('email', $secondUser->email);

$this->assertTrue($firstUser->is(UserWhereTest::firstWhereOrFail(['name' => 'test-name', 'email' => 'test-email'])));

$this->expectException(ModelNotFoundException::class);
UserWhereTest::firstWhereOrFail(['name' => 'test-name', 'email' => 'test-email1']);

$this->assertTrue($secondUser->is(
UserWhereTest::firstWhereOrFail(['name' => 'wrong-name', 'email' => 'test-email1'], null, null, 'or'))
);
}
}

class UserWhereTest extends Model
Expand Down