GithubHelp home page GithubHelp logo

insert-on-duplicate-key's Introduction

MySQL Insert On Duplicate Key Update Eloquent Trait

Latest Stable Version License Build Status

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

use Illuminate\Database\Eloquent\Model;
use Yadakhov\InsertOnDuplicateKey;

/**
 * Class User.
 */
class User extends Model
{
    // The function is implemented as a trait.
    use InsertOnDuplicateKey;
}

Multi values insert.

    $users = [
        ['id' => 1, 'email' => '[email protected]', 'name' => 'User One'],
        ['id' => 2, 'email' => '[email protected]', 'name' => 'User Two'],
        ['id' => 3, 'email' => '[email protected]', 'name' => 'User Three'],
    ];

INSERT ON DUPLICATE KEY UPDATE

    User::insertOnDuplicateKey($users);
    -- produces this query
    INSERT INTO `users`(`id`,`email`,`name`) VALUES
    (1,'[email protected]','User One'), (2,'[email protected]','User Two'), (3,'user3email.com','User Three')
    ON DUPLICATE KEY UPDATE `id` = VALUES(`id`), `email` = VALUES(`email`), `name` = VALUES(`name`)
    User::insertOnDuplicateKey($users, ['email']);
    -- produces this query
    INSERT INTO `users`(`id`,`email`,`name`) VALUES
    (1,'[email protected]','User One'), (2,'[email protected]','User Two'), (3,'user3email.com','User Three')
    ON DUPLICATE KEY UPDATE `email` = VALUES(`email`)

INSERT IGNORE

    User::insertIgnore($users);
    -- produces this query
    INSERT IGNORE INTO `users`(`id`,`email`,`name`) VALUES
    (1,'[email protected]','User One'), (2,'[email protected]','User Two'), (3,'user3email.com','User Three');

REPLACE INTO

    User::replace($users);
    -- produces this query
    REPLACE INTO `users`(`id`,`email`,`name`) VALUES
    (1,'[email protected]','User One'), (2,'[email protected]','User Two'), (3,'user3email.com','User Three');

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.

['id' => 1, 'email' => '[email protected]', 'name' => 'User One', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]

Run unit tests

./vendor/bin/phpunit

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.

insert-on-duplicate-key's People

Contributors

hibob224 avatar jstoks avatar kkomelin avatar votemike avatar yadakhov avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.