Skip to content

Commit 7cf28fd

Browse files
committed
Ensure createOrFirst does not return null
1 parent 4c5d4d5 commit 7cf28fd

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
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) {
585-
return $this->useWritePdo()->where($attributes)->first();
585+
return $this->useWritePdo()->where($attributes)->firstOrFail();
586586
}
587587
}
588588

src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -653,11 +653,11 @@ public function createOrFirst(array $attributes = [], array $values = [], array
653653
}
654654

655655
try {
656-
return tap($this->related->where($attributes)->first(), function ($instance) use ($joining, $touch) {
656+
return tap($this->related->where($attributes)->firstOrFail(), function ($instance) use ($joining, $touch) {
657657
$this->getQuery()->withSavepointIfNeeded(fn () => $this->attach($instance, $joining, $touch));
658658
});
659659
} catch (UniqueConstraintViolationException) {
660-
return (clone $this)->useWritePdo()->where($attributes)->first();
660+
return (clone $this)->useWritePdo()->where($attributes)->firstOrFail();
661661
}
662662
}
663663

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) {
257-
return $this->useWritePdo()->where($attributes)->first();
257+
return $this->useWritePdo()->where($attributes)->firstOrFail();
258258
}
259259
}
260260

0 commit comments

Comments
 (0)