Skip to content

Commit 27958eb

Browse files
authored
[10.x] Ensuring Primary Reference on Retry in createOrFirst() (#48161)
* fix: πŸ› Ensuring Primary Reference on Retry in `createOrFirst()` * test: πŸ’ Fix tests * fix: πŸ› Apply `useWritePdo()` to all `createOrFirst` impls
1 parent 3365194 commit 27958eb

File tree

5 files changed

+6
-3
lines changed

5 files changed

+6
-3
lines changed

β€Žsrc/Illuminate/Database/Eloquent/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ public function createOrFirst(array $attributes = [], array $values = [])
582582
try {
583583
return $this->withSavepointIfNeeded(fn () => $this->create(array_merge($attributes, $values)));
584584
} catch (UniqueConstraintViolationException $exception) {
585-
return $this->where($attributes)->first();
585+
return $this->useWritePdo()->where($attributes)->first();
586586
}
587587
}
588588

β€Žsrc/Illuminate/Database/Eloquent/Relations/BelongsToMany.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ public function createOrFirst(array $attributes = [], array $values = [], array
653653
$this->getQuery()->withSavepointIfNeeded(fn () => $this->attach($instance, $joining, $touch));
654654
});
655655
} catch (UniqueConstraintViolationException $exception) {
656-
return (clone $this)->where($attributes)->first();
656+
return (clone $this)->useWritePdo()->where($attributes)->first();
657657
}
658658
}
659659

β€Žsrc/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public function createOrFirst(array $attributes = [], array $values = [])
254254
try {
255255
return $this->getQuery()->withSavepointIfNeeded(fn () => $this->create(array_merge($attributes, $values)));
256256
} catch (UniqueConstraintViolationException $exception) {
257-
return $this->where($attributes)->first();
257+
return $this->useWritePdo()->where($attributes)->first();
258258
}
259259
}
260260

β€Žtests/Database/DatabaseEloquentHasManyTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ public function testCreateOrFirstMethodWithValuesFindsFirstModel()
184184
$relation->getQuery()->shouldReceive('withSavepointIfNeeded')->once()->andReturnUsing(function ($scope) {
185185
return $scope();
186186
});
187+
$relation->getQuery()->shouldReceive('useWritePdo')->once()->andReturn($relation->getQuery());
187188
$relation->getQuery()->shouldReceive('where')->once()->with(['foo' => 'bar'])->andReturn($relation->getQuery());
188189
$relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock(stdClass::class));
189190

β€Žtests/Database/DatabaseEloquentMorphTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ public function testCreateOrFirstMethodFindsFirstModel()
230230
$relation->getQuery()->shouldReceive('withSavepointIfNeeded')->once()->andReturnUsing(function ($scope) {
231231
return $scope();
232232
});
233+
$relation->getQuery()->shouldReceive('useWritePdo')->once()->andReturn($relation->getQuery());
233234
$relation->getQuery()->shouldReceive('where')->once()->with(['foo'])->andReturn($relation->getQuery());
234235
$relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock(Model::class));
235236

@@ -250,6 +251,7 @@ public function testCreateOrFirstMethodWithValuesFindsFirstModel()
250251
$relation->getQuery()->shouldReceive('withSavepointIfNeeded')->once()->andReturnUsing(function ($scope) {
251252
return $scope();
252253
});
254+
$relation->getQuery()->shouldReceive('useWritePdo')->once()->andReturn($relation->getQuery());
253255
$relation->getQuery()->shouldReceive('where')->once()->with(['foo' => 'bar'])->andReturn($relation->getQuery());
254256
$relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock(Model::class));
255257

0 commit comments

Comments
Β (0)