Download the PHP package yadakhov/insert-on-duplicate-key without Composer
On this page you can find all versions of the php package yadakhov/insert-on-duplicate-key. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download yadakhov/insert-on-duplicate-key
More information about yadakhov/insert-on-duplicate-key
Files in yadakhov/insert-on-duplicate-key
Package insert-on-duplicate-key
Short Description A trait for MySQL INSERT ON DUPLICATE KEY UPDATE.
License MIT
Homepage https://github.com/yadakhov/insert-on-duplicate-key
Informations about the package insert-on-duplicate-key
MySQL Insert On Duplicate Key Update Eloquent Trait
Insert Duplicate Key Update is a quick way to do mass insert.
It's a trait meant to be used with Laravel's Eloquent ORM.
Code Example
Multi values insert.
Important: the order of the keys are important. It should be the same for every arrays.
The reason is the code uses array_values()
.
Do not do this:
INSERT ON DUPLICATE KEY UPDATE
If users have a numeric column we would like, for example, to sum:
INSERT IGNORE
REPLACE INTO
created_at and updated_at fields.
created_at and updated_at will not be updated automatically. To update you can pass the fields in the insert array.
Run unit tests
Will this work on Postgresql?
No. On Duplicate Key Update is only available on MySQL. Postgresql 9.4 has a similar feature called UPSERT. Implementing UPSERT is left as an exercise for the reader.
Isn't this the same as updateOrCreate()?
It is similar but not the same. The updateOrCreate() will only work for one row at a time which doesn't allow bulk insert. InsertOnDuplicateKey will work on many rows.