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
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,8 @@ public function createOrFirst(array $attributes = [], array $values = [])
{
try {
return $this->withSavepointIfNeeded(fn () => $this->create(array_merge($attributes, $values)));
} catch (UniqueConstraintViolationException) {
return $this->useWritePdo()->where($attributes)->first();
} catch (UniqueConstraintViolationException $e) {
return $this->useWritePdo()->where($attributes)->first() ?? throw $e;
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -648,16 +648,16 @@ public function createOrFirst(array $attributes = [], array $values = [], array
{
try {
return $this->getQuery()->withSavePointIfNeeded(fn () => $this->create(array_merge($attributes, $values), $joining, $touch));
} catch (UniqueConstraintViolationException $exception) {
} catch (UniqueConstraintViolationException $e) {
// ...
}

try {
return tap($this->related->where($attributes)->first(), function ($instance) use ($joining, $touch) {
return tap($this->related->where($attributes)->first() ?? throw $e, function ($instance) use ($joining, $touch) {
$this->getQuery()->withSavepointIfNeeded(fn () => $this->attach($instance, $joining, $touch));
});
} catch (UniqueConstraintViolationException) {
return (clone $this)->useWritePdo()->where($attributes)->first();
} catch (UniqueConstraintViolationException $e) {
return (clone $this)->useWritePdo()->where($attributes)->first() ?? throw $e;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ public function createOrFirst(array $attributes = [], array $values = [])
{
try {
return $this->getQuery()->withSavepointIfNeeded(fn () => $this->create(array_merge($attributes, $values)));
} catch (UniqueConstraintViolationException) {
return $this->useWritePdo()->where($attributes)->first();
} catch (UniqueConstraintViolationException $e) {
return $this->useWritePdo()->where($attributes)->first() ?? throw $e;
}
}

Expand Down
24 changes: 24 additions & 0 deletions tests/Database/DatabaseEloquentIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Database\QueryException;
use Illuminate\Database\UniqueConstraintViolationException;
use Illuminate\Pagination\AbstractPaginator as Paginator;
use Illuminate\Pagination\Cursor;
use Illuminate\Pagination\CursorPaginator;
Expand Down Expand Up @@ -91,6 +92,8 @@ protected function createSchema()
$this->schema($connection)->create('unique_users', function ($table) {
$table->increments('id');
$table->string('name')->nullable();
// Unique constraint will be applied only for non-null values
$table->string('screen_name')->nullable()->unique();
$table->string('email')->unique();
$table->timestamp('birthday', 6)->nullable();
$table->timestamps();
Expand Down Expand Up @@ -552,6 +555,27 @@ public function testCreateOrFirst()
$this->assertSame('Nuno Maduro', $user4->name);
}

public function testCreateOrFirstNonAttributeFieldViolation()
{
// 'email' and 'screen_name' are unique and independent of each other.
EloquentTestUniqueUser::create([
'email' => '[email protected]',
'screen_name' => '@taylorotwell',
]);

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

// Although 'email' is expected to be unique and is passed as $attributes,
// if the 'screen_name' attribute listed in non-unique $values causes a violation,
// a UniqueConstraintViolationException should be thrown.
EloquentTestUniqueUser::createOrFirst(
['email' => '[email protected]'],
[
'screen_name' => '@taylorotwell',
]
);
}

public function testCreateOrFirstWithinTransaction()
{
$user1 = EloquentTestUniqueUser::create(['email' => '[email protected]']);
Expand Down