-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[10.x] Add upsertUsing
method to query builder
#50305
Conversation
Tried using it on a real SQLite database - failed with invalid syntax:
DB::table('flights')->upsertUsing(['origin', 'destination', 'duration'], function ($query) {
$query->select(['origin', 'destination', 'duration'])
->from('old_flights');
}, ['origin', 'destination'], ['duration']); Mark as ready for review if you want me to look again. |
Found the problem described in the documentation under "2.2. Parsing Ambiguity". |
But it raises questions in my mind of what this has actually been tested against if the most basic example didn't work? 😅 |
Good point. Somehow I forgot to test against SQLite, but tested against other databases (even MS SQL Server which I haven't used before). I'm now finding out about a limitation with SQL server if f.e. "ORDER BY" is to be used, subquery also needs to contain "OFFSET". I'll convert to draft and mark ready for review once the changes are done. |
Please send this to 11.x instead, thanks. |
Currently we have
upsert
method for operations where we want to insert new records and update existing ones based on some unique identifier.upsert
, similarly toinsert
, uses provided array of values to insert and update the table.To insert using a subquery as source of data instead of an array we have
insertUsing
method. So, in a similar fashion,upsertUsing
method that this PR is adding, uses a subquery as source of data to perform "upsert".I can get something like this for my projects using macros, but I thought that it could be useful to others, hence this PR.
There are no breaking changes, only new functionality.
If there's something to improve before this PR can be accepted, please let me know and I'll make the changes.
Cheers!