<commit_before># Laravel Insert On Duplicate Key And Insert Ignore

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.

## Installation

Install this package with composer.

```sh
composer require guidocella/eloquent-insert-on-duplicate-key
```

If you don't use Package Auto-Discovery yet add the service provider to your Package Service Providers in `config/app.php`.

```php
InsertOnDuplicateKey\InsertOnDuplicateKeyServiceProvider::class,
```

## Usage

### Models

Call `insertOnDuplicateKey` or `insertIgnore` from a model with the array of data to insert in its table.

```php
$data = [
    ['id' => 1, 'name' => 'name1', 'email' => '<EMAIL>'],
    ['id' => 2, 'name' => 'name2', 'email' => '<EMAIL>'],
];

User::insertOnDuplicateKey($data);

User::insertIgnore($data);
```

#### 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.

```php
User::insertOnDuplicateKey([
    'id'    => 1,
    'name'  => 'new name',
    'email' => '<EMAIL>',
], ['name']);
// The name will be updated but not the email.
```

##### Update with custom values

You also 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'.

```php
User::insertOnDuplicateKey([
    'id'    => 1,
    'name'  => 'created user',
], ['name' => 'updated user']);
```

The generated SQL is:

```sql
INSERT INTO `users` (`id`, `name`) VALUES (1, "created user") ON DUPLICATE KEY UPDATE `name` = 'updated user'<commit_msg>Wrong word<commit_after># Laravel Insert On Duplicate Key And Insert Ignore

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.

## Installation

Install this package with composer.

```sh
composer require guidocella/eloquent-insert-on-duplicate-key
```

If you don't use Package Auto-Discovery yet add the service provider to your Package Service Providers in `config/app.php`.

```php
InsertOnDuplicateKey\InsertOnDuplicateKeyServiceProvider::class,
```

## Usage

### Models

Call `insertOnDuplicateKey` or `insertIgnore` from a model with the array of data to insert in its table.

```php
$data = [
    ['id' => 1, 'name' => 'name1', 'email' => '<EMAIL>'],
    ['id' => 2, 'name' => 'name2', 'email' => '<EMAIL>'],
];

User::insertOnDuplicateKey($data);

User::insertIgnore($data);
```

#### 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.

```php
User::insertOnDuplicateKey([
    'id'    => 1,
    'name'  => 'new name',
    'email' => '<EMAIL>',
], ['name']);
// The name will be updated but not the email.
```

##### 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'.

```php
User::insertOnDuplicateKey([
    'id'    => 1,
    'name'  => 'created user',
], ['name' => 'updated user']);
```

The generated SQL is:

```sql
INSERT INTO `users` (`id`, `name`) VALUES (1, "created user") ON DUPLICATE KEY UPDATE `name` = 'updated user'
{'fraction_non_alphanumeric': 0.13441734417344173, 'fraction_numerical': 0.004878048780487805, 'mean_word_length': 4.30316091954023, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 33, 'https://': 0, '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': '152572', 'n_tokens_mistral': 1179, 'n_tokens_neox': 1094, 'n_words': 466}