<commit_before># MySQL Insert On Duplicate Key Update Eloquent Trait

[![Latest Stable Version](https://poser.pugx.org/yadakhov/insert-on-duplicate-key/version)](https://packagist.org/packages/yadakhov/insert-on-duplicate-key)
[![License](https://poser.pugx.org/yadakhov/insert-on-duplicate-key/license)](https://packagist.org/packages/yadakhov/insert-on-duplicate-key)
[![Build Status](https://travis-ci.org/yadakhov/insert-on-duplicate-key.svg)](https://travis-ci.org/yadakhov/insert-on-duplicate-key)

[Insert Duplicate Key Update](http://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html) is a quick way to do mass insert.

It's a trait meant to be used with Laravel's Eloquent ORM.

### Code Example

```php
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.
```php
    $users = [
        ['id' => 1, 'email' => '<EMAIL>', 'name' => 'User One'],
        ['id' => 2, 'email' => '<EMAIL>', 'name' => '<NAME>'],
        ['id' => 3, 'email' => '<EMAIL>', 'name' => '<NAME>'],
    ];
```

#### INSERT ON DUPLICATE KEY UPDATE

```php
    User::insertOnDuplicateKey($users);
```
```sql
    -- produces this query
    INSERT INTO `users`(`id`,`email`,`name`) VALUES
    (1,'<EMAIL>','User One'), (2,'<EMAIL>','User Two'), (3,'<EMAIL>','User Three')
    ON DUPLICATE KEY UPDATE `id` = VALUES(`id`), `email` = VALUES(`email`), `name` = VALUES(`name`)
```

```php
    User::insertOnDuplicateKey($users, ['email']);
```
```sql
    -- produces this query
    INSERT INTO `users`(`id`,`email`,`name`) VALUES
    (1,'<EMAIL>','<NAME>'), (2,'<EMAIL>','<NAME>'), (3,'<EMAIL>','User Three')
    ON DUPLICATE KEY UPDATE `email` = VALUES(`email`)<commit_msg>Add documentation for the ordering of the keys is important<commit_after># MySQL Insert On Duplicate Key Update Eloquent Trait

[![Latest Stable Version](https://poser.pugx.org/yadakhov/insert-on-duplicate-key/version)](https://packagist.org/packages/yadakhov/insert-on-duplicate-key)
[![License](https://poser.pugx.org/yadakhov/insert-on-duplicate-key/license)](https://packagist.org/packages/yadakhov/insert-on-duplicate-key)
[![Build Status](https://travis-ci.org/yadakhov/insert-on-duplicate-key.svg)](https://travis-ci.org/yadakhov/insert-on-duplicate-key)

[Insert Duplicate Key Update](http://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html) is a quick way to do mass insert.

It's a trait meant to be used with Laravel's Eloquent ORM.

### Code Example

```php
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.
```php
    $users = [
        ['id' => 1, 'email' => '<EMAIL>', 'name' => '<NAME>'],
        ['id' => 2, 'email' => '<EMAIL>', 'name' => '<NAME>'],
        ['id' => 3, 'email' => '<EMAIL>', 'name' => 'User Three'],
    ];
```

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:

```php
    $users = [
        ['id' => 1, 'email' => '<EMAIL>', 'name' => '<NAME>'],
        ['email' => '<EMAIL>', 'id' => 2, 'name' => '<NAME>'],
        ['email' => '<EMAIL>', 'name' => '<NAME>', 'id' => 3],
    ];
```


#### INSERT ON DUPLICATE KEY UPDATE

```php
    User::insertOnDuplicateKey($users);
```
```sql
    -- produces this query
    INSERT INTO `users`(`id`,`email`,`name`) VALUES
    (1,'<EMAIL>','User One'), (2,'<EMAIL>','User Two'), (3,'<EMAIL>','User Three')
    ON DUPLICATE KEY UPDATE `id` = VALUES(`id`), `email` = VALUES(`email`), `name` = VALUES(`name`)
```

```php
    User::insertOnDuplicateKey($users, ['email']);
```
```sql
    -- produces this query
    INSERT INTO `users`(`id`,`email`,`name`) VALUES
    (1,'<EMAIL>','User One'), (2,'<EMAIL>','User Two'), (3,'<EMAIL>','User Three')
    ON DUPLICATE KEY UPDATE `email` = VALUES(`email`)
{'fraction_non_alphanumeric': 0.21769868356899075, 'fraction_numerical': 0.006094588005850805, 'mean_word_length': 4.084262701363073, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 60, 'https://': 12, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6973449', 'n_tokens_mistral': 1535, 'n_tokens_neox': 1454, 'n_words': 329}