diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index 02a5d60eef16..ba5a678b81e4 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -582,7 +582,7 @@ 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(); + return $this->useWritePdo()->where($attributes)->firstOrFail(); } } diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php index b2e494478211..7338bae4f313 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php @@ -653,11 +653,11 @@ public function createOrFirst(array $attributes = [], array $values = [], array } try { - return tap($this->related->where($attributes)->first(), function ($instance) use ($joining, $touch) { + return tap($this->related->where($attributes)->firstOrFail(), function ($instance) use ($joining, $touch) { $this->getQuery()->withSavepointIfNeeded(fn () => $this->attach($instance, $joining, $touch)); }); } catch (UniqueConstraintViolationException) { - return (clone $this)->useWritePdo()->where($attributes)->first(); + return (clone $this)->useWritePdo()->where($attributes)->firstOrFail(); } } diff --git a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php index c748dd7c644d..6e82a82d425c 100755 --- a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php @@ -254,7 +254,7 @@ 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(); + return $this->useWritePdo()->where($attributes)->firstOrFail(); } } diff --git a/tests/Database/DatabaseEloquentHasManyTest.php b/tests/Database/DatabaseEloquentHasManyTest.php index b184dfdd14c4..ed6aa5a0feab 100755 --- a/tests/Database/DatabaseEloquentHasManyTest.php +++ b/tests/Database/DatabaseEloquentHasManyTest.php @@ -188,7 +188,7 @@ public function testCreateOrFirstMethodWithValuesFindsFirstModel() }); $relation->getQuery()->shouldReceive('useWritePdo')->once()->andReturn($relation->getQuery()); $relation->getQuery()->shouldReceive('where')->once()->with(['foo' => 'bar'])->andReturn($relation->getQuery()); - $relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock(stdClass::class)); + $relation->getQuery()->shouldReceive('firstOrFail')->once()->with()->andReturn($model = m::mock(stdClass::class)); $this->assertInstanceOf(stdClass::class, $found = $relation->createOrFirst(['foo' => 'bar'], ['baz' => 'qux'])); $this->assertSame($model, $found); diff --git a/tests/Database/DatabaseEloquentMorphTest.php b/tests/Database/DatabaseEloquentMorphTest.php index 4870284090f2..2d70682c52c8 100755 --- a/tests/Database/DatabaseEloquentMorphTest.php +++ b/tests/Database/DatabaseEloquentMorphTest.php @@ -234,7 +234,7 @@ public function testCreateOrFirstMethodFindsFirstModel() }); $relation->getQuery()->shouldReceive('useWritePdo')->once()->andReturn($relation->getQuery()); $relation->getQuery()->shouldReceive('where')->once()->with(['foo'])->andReturn($relation->getQuery()); - $relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock(Model::class)); + $relation->getQuery()->shouldReceive('firstOrFail')->once()->with()->andReturn($model = m::mock(Model::class)); $this->assertInstanceOf(Model::class, $relation->createOrFirst(['foo'])); } @@ -255,7 +255,7 @@ public function testCreateOrFirstMethodWithValuesFindsFirstModel() }); $relation->getQuery()->shouldReceive('useWritePdo')->once()->andReturn($relation->getQuery()); $relation->getQuery()->shouldReceive('where')->once()->with(['foo' => 'bar'])->andReturn($relation->getQuery()); - $relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock(Model::class)); + $relation->getQuery()->shouldReceive('firstOrFail')->once()->with()->andReturn($model = m::mock(Model::class)); $this->assertInstanceOf(Model::class, $relation->createOrFirst(['foo' => 'bar'], ['baz' => 'qux'])); }