diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index 2e17922c5353..7e4b2725bd2b 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 $exception) { - return $this->where($attributes)->first(); + return $this->useWritePdo()->where($attributes)->first(); } } diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php index 4a443ddece10..2e1078a609aa 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php @@ -653,7 +653,7 @@ public function createOrFirst(array $attributes = [], array $values = [], array $this->getQuery()->withSavepointIfNeeded(fn () => $this->attach($instance, $joining, $touch)); }); } catch (UniqueConstraintViolationException $exception) { - return (clone $this)->where($attributes)->first(); + return (clone $this)->useWritePdo()->where($attributes)->first(); } } diff --git a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php index 109cb017dc95..c4b52db7ff3e 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 $exception) { - return $this->where($attributes)->first(); + return $this->useWritePdo()->where($attributes)->first(); } } diff --git a/tests/Database/DatabaseEloquentHasManyTest.php b/tests/Database/DatabaseEloquentHasManyTest.php index a10e884cd6e9..0bec03bc97fa 100755 --- a/tests/Database/DatabaseEloquentHasManyTest.php +++ b/tests/Database/DatabaseEloquentHasManyTest.php @@ -184,6 +184,7 @@ public function testCreateOrFirstMethodWithValuesFindsFirstModel() $relation->getQuery()->shouldReceive('withSavepointIfNeeded')->once()->andReturnUsing(function ($scope) { return $scope(); }); + $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)); diff --git a/tests/Database/DatabaseEloquentMorphTest.php b/tests/Database/DatabaseEloquentMorphTest.php index ed17b3a092a5..924cd17fc97b 100755 --- a/tests/Database/DatabaseEloquentMorphTest.php +++ b/tests/Database/DatabaseEloquentMorphTest.php @@ -230,6 +230,7 @@ public function testCreateOrFirstMethodFindsFirstModel() $relation->getQuery()->shouldReceive('withSavepointIfNeeded')->once()->andReturnUsing(function ($scope) { return $scope(); }); + $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)); @@ -250,6 +251,7 @@ public function testCreateOrFirstMethodWithValuesFindsFirstModel() $relation->getQuery()->shouldReceive('withSavepointIfNeeded')->once()->andReturnUsing(function ($scope) { return $scope(); }); + $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));