2017 © Pedro Peláez
 

library eloquent-insert-on-duplicate-key

Macros for INSERT ON DUPLICATE KEY UPDATE and INSERT IGNORE with Eloquent

image

guidocella/eloquent-insert-on-duplicate-key

Macros for INSERT ON DUPLICATE KEY UPDATE and INSERT IGNORE with Eloquent

  • Wednesday, February 21, 2018
  • by guidocella
  • Repository
  • 4 Watchers
  • 24 Stars
  • 8,799 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 8 Forks
  • 0 Open issues
  • 9 Versions
  • 33 % Grown

The README.md

Laravel Insert On Duplicate Key And Insert Ignore

This package is deprecated because upsert and insertOrIgnore have been added to Laravel., (*1)

If you need the pivot functions, they are trivial to implement:, (*2)

BelongsToMany::macro('attachUpsert', function ($id, array $attributes = []) {
    $this->newPivotStatement()->upsert($this->formatAttachRecords(
        $this->parseIds($id),
        $attributes
    ), null);
});

BelongsToMany::macro('attachOrIgnore', function ($id, array $attributes = []) {
    $this->newPivotStatement()->insertOrIgnore($this->formatAttachRecords(
        $this->parseIds($id),
        $attributes
    ));
});

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., (*3)

Installation

Install this package with composer., (*4)

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., (*5)

InsertOnDuplicateKey\InsertOnDuplicateKeyServiceProvider::class,

Usage

Models

Call insertOnDuplicateKey or insertIgnore from a model with the array of data to insert in its table., (*6)

$data = [
    ['id' => 1, 'name' => 'name1', 'email' => 'user1@email.com'],
    ['id' => 2, 'name' => 'name2', 'email' => 'user2@email.com'],
];

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., (*7)

User::insertOnDuplicateKey([
    'id'    => 1,
    'name'  => 'new name',
    'email' => 'foo@gmail.com',
], ['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., (*8)

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'., (*9)

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

The generated SQL is:, (*10)

INSERT INTO `users` (`id`, `name`) VALUES (1, "created user") ON DUPLICATE KEY UPDATE `name` = "updated user"

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:, (*11)

User::insertOnDuplicateKey([
    'id'       => 1,
    'name'     => 'created user',
    'email'    => 'new@gmail.com',
    'password' => 'secret',
], ['name' => 'updated user', 'email']);

will generate, (*12)

INSERT INTO `users` (`id`, `name`, `email`, `password`)
VALUES (1, "created user", "new@gmail.com", "secret")
ON DUPLICATE KEY UPDATE `name` = "updated user", `email` = VALUES(`email`)

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., (*13)

$pivotData = [
    1 => ['expires_at' => Carbon::today()],
    2 => ['expires_at' => Carbon::tomorrow()],
];

$user->roles()->attachOnDuplicateKey($pivotData);

$user->roles()->attachIgnore($pivotData);

The Versions

21/02 2018

dev-master

9999999-dev

Macros for INSERT ON DUPLICATE KEY UPDATE and INSERT IGNORE with Eloquent

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent mysql

17/02 2018

v2.2

2.2.0.0

Macros for INSERT ON DUPLICATE KEY UPDATE and INSERT IGNORE with Eloquent

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent mysql

08/02 2018

v2.1.1

2.1.1.0

Macros for INSERT ON DUPLICATE KEY UPDATE and INSERT IGNORE with Eloquent

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent mysql

23/10 2017

v2.1

2.1.0.0

Macros for INSERT ON DUPLICATE KEY UPDATE and INSERT IGNORE with Eloquent

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent mysql

28/09 2017

v2.0

2.0.0.0

Macros for INSERT ON DUPLICATE KEY UPDATE and INSERT IGNORE with Eloquent

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent mysql

30/08 2017

v1.0.2

1.0.2.0

Macros for INSERT ON DUPLICATE KEY UPDATE and INSERT IGNORE with Eloquent

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent mysql

21/01 2017

v1.0.1

1.0.1.0

Macros for INSERT ON DUPLICATE KEY UPDATE and INSERT IGNORE with Eloquent

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent mysql

15/12 2016

v1.0.0

1.0.0.0

Macros for INSERT ON DUPLICATE KEY UPDATE and INSERT IGNORE with Eloquent

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent mysql

10/12 2016

v0.0.0

0.0.0.0

Functions for INSERT ON DUPLICATE KEY UPDATE and INSERT IGNORE with Eloquent

  Sources   Download

MIT

The Requires

 

database laravel eloquent mysql