Download the PHP package guidocella/eloquent-insert-on-duplicate-key without Composer
On this page you can find all versions of the php package guidocella/eloquent-insert-on-duplicate-key. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download guidocella/eloquent-insert-on-duplicate-key
More information about guidocella/eloquent-insert-on-duplicate-key
Files in guidocella/eloquent-insert-on-duplicate-key
Package eloquent-insert-on-duplicate-key
Short Description Macros for INSERT ON DUPLICATE KEY UPDATE and INSERT IGNORE with Eloquent
License MIT
Informations about the package eloquent-insert-on-duplicate-key
Laravel Insert On Duplicate Key And Insert Ignore
This package is deprecated because upsert
and insertOrIgnore
have been added to Laravel.
If you need the pivot functions, they are trivial to implement:
This package provides macros to run INSERT ... ON DUPLICATE KEY UPDATE and INSERT IGNORE queries on models and pivot tables with Laravel's ORM Eloquent using MySql or MariaDB.
Installation
Install this package with composer.
If you don't use Package Auto-Discovery yet add the service provider to your Package Service Providers in config/app.php
.
Usage
Models
Call insertOnDuplicateKey
or insertIgnore
from a model with the array of data to insert in its table.
Customizing the ON DUPLICATE KEY UPDATE clause
Update only certain columns
If you want to update only certain columns, pass them as the 2nd argument.
Update with custom values
You can customize the value with which the columns will be updated when a row already exists by passing an associative array.
In the following example, if a user with id = 1 doesn't exist, it will be created with name = 'created user'. If it already exists, it will be updated with name = 'updated user'.
The generated SQL is:
You may combine key/value pairs and column names in the 2nd argument to specify the columns to update with a custom literal or expression or with the default VALUES(column)
. For example:
will generate
Pivot tables
Call attachOnDuplicateKey
and attachIgnore
from a BelongsToMany
relation to run the inserts in its pivot table. You can pass the data in any of the formats accepted by attach
.