2017 © Pedro Peláez
 

library eloquent-insert-on-duplicate-key

A trait for MySQL INSERT ON DUPLICATE KEY UPDATE with Eloquent.

image

talentasia/eloquent-insert-on-duplicate-key

A trait for MySQL INSERT ON DUPLICATE KEY UPDATE with Eloquent.

  • Friday, August 4, 2017
  • by thosuperman
  • Repository
  • 1 Watchers
  • 0 Stars
  • 57 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 16 % Grown

The README.md

MySQL Insert On Duplicate Key Update Eloquent Trait

Insert Duplicate Key Update is a quick way to do mass insert., (*1)

It's a trait meant to be used with Laravel's Eloquent ORM., (*2)

Code Example

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

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

Multi values insert.

    $users = [
        ['id' => 1, 'email' => 'user1@email.com', 'name' => 'User 1'],
        ['id' => 2, 'email' => 'user2@email.com', 'name' => 'User 2'],
        ['id' => 3, 'email' => 'user3@email.com', 'name' => 'User 3'],
    ];

INSERT ON DUPLICATE KEY UPDATE

    User::insertOnDuplicateKey($users);

```sql -- produces this query INSERT INTO users(id,email,name) VALUES (1,'user1@email.com','User One'), (2,'user3@email.com','User Two'), (3,'user3email.com','User Three') ON DUPLICATE KEY UPDATE id = VALUES(id), email = VALUES(email), name = VALUES(name), (*3)


```php User::insertOnDuplicateKey($users, ['email']);

```sql -- produces this query INSERT INTO users(id,email,name) VALUES (1,'user1@email.com','User One'), (2,'user3@email.com','User Two'), (3,'user3email.com','User Three') ON DUPLICATE KEY UPDATE email = VALUES(email), (*4)


#### INSERT IGNORE ```php User::insertIgnore($users);

```sql -- produces this query INSERT IGNORE INTO users(id,email,name) VALUES (1,'user1@email.com','User One'), (2,'user3@email.com','User Two'), (3,'user3email.com','User Three');, (*5)


#### REPLACE INTO ```php User::replace($users);

```sql -- produces this query REPLACE INTO users(id,email,name) VALUES (1,'user1@email.com','User One'), (2,'user3@email.com','User Two'), (3,'user3email.com','User Three');, (*6)


### 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. ```php ['id' => 1, 'email' => 'user1@email.com', '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., (*7)

The Versions

04/08 2017

dev-master

9999999-dev https://github.com/thosuperman/eloquent-insert-on-duplicate-key

A trait for MySQL INSERT ON DUPLICATE KEY UPDATE with Eloquent.

  Sources   Download

MIT

The Development Requires

database eloquent mysql

04/08 2017

0.1

0.1.0.0 https://github.com/thosuperman/eloquent-insert-on-duplicate-key

A trait for MySQL INSERT ON DUPLICATE KEY UPDATE with Eloquent.

  Sources   Download

MIT

The Development Requires

database eloquent mysql