You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The current implementation of createOrFirst() in Laravel's Eloquent Builder has a behavior that could lead to unintended consequences when there are multiple unique constraints.
Current Behavior
The createOrFirst() method tries to create a record first and, if a UniqueConstraintViolationException occurs, it falls back to finding the record based on $attributes.
Assume that we have two columns id_a and id_b, both of which are unique, and id_a is the primary key.
If the database already has the following records:
(id_a, id_b) = [(1, 100), (2, 200), (3, 300), (4, 400)]
And we call createOrFirst(['id_a' => 5], ['id_b' => 400])
The create() call in the try block will fail because of id_b being unique. However, the catch block only looks for a record where id_a = 5, which will return NULL.
Steps To Reproduce
Create a database table with two unique columns, for example id_a and id_b. Make id_a the primary key.
Laravel Version
10.21.0
PHP Version
8.2.8
Database Driver & Version
No response
Description
Summary
The current implementation of createOrFirst() in Laravel's Eloquent Builder has a behavior that could lead to unintended consequences when there are multiple unique constraints.
Current Behavior
The createOrFirst() method tries to create a record first and, if a UniqueConstraintViolationException occurs, it falls back to finding the record based on $attributes.
Here is the current implementation:
framework/src/Illuminate/Database/Eloquent/Builder.php
Lines 580 to 587 in 96b15c7
Issue
Assume that we have two columns id_a and id_b, both of which are unique, and id_a is the primary key.
If the database already has the following records:
(id_a, id_b) = [(1, 100), (2, 200), (3, 300), (4, 400)]
And we call createOrFirst(['id_a' => 5], ['id_b' => 400])
The create() call in the try block will fail because of id_b being unique. However, the catch block only looks for a record where id_a = 5, which will return NULL.
Steps To Reproduce
The text was updated successfully, but these errors were encountered: