New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

guidocella/eloquent-insert-on-duplicate-key

Macros for INSERT ON DUPLICATE KEY UPDATE and INSERT IGNORE with Eloquent
604,802 87

Larablocks Analysis

guidocella/eloquent-insert-on-duplicate-key works with Laravel 5 to 8 (latest release v2.2.5, Sep 10, 2020), all past end of life (the last on Jan 24, 2023): no more fixes, even for security.

Recommendation checks

Replacement recommended

Replacement recommended: use laravel/framework instead. Named by its author on Packagist.

  • Marked abandoned on Packagist
  • Supports only Laravel 5 to 8, past end of life since Jan 24, 2023
  • No release in the last 12 months (last: Sep 10, 2020)
  • 1,604 downloads a month (needs 10,000)
  • 87 GitHub stars (needs 100)

State

Abandoned
  • Packagist marks this package abandoned.
  • Last stable release: Sep 10, 2020.
  • No releases in the last 12 months.
  • First released: Dec 10, 2016.
  • Monthly downloads: 1,604.
  • 0 open issues on GitHub.

Data from Packagist and GitHub, checked by Larablocks on Dec 3, 2026.

Install
composer require guidocella/eloquent-insert-on-duplicate-key
Latest Version:v2.2.5
PHP:>=5.6.4
Laravel:5 – 8
License:MIT
Last Updated:Oct 15, 2020
Links: GitHub  ·  Packagist
Maintainer: guidocella

Laravel Insert On Duplicate Key And Insert Ignore

This package is deprecated because upsert and insertOrIgnore have been added to Laravel.

If you need the pivot functions, they are trivial to implement:

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.

Installation

Install this package with composer.

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.

InsertOnDuplicateKey\InsertOnDuplicateKeyServiceProvider::class,

Usage

Models

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

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

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.

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

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

The generated SQL is:

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:

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

will generate

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.

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

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

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

Related Packages

yadakhov/insert-on-duplicate-key

A trait for MySQL INSERT ON DUPLICATE KEY UPDATE.

3,149,973 275
zara-4/laravel-lazy-mysql

A lazy mysql based Eloquent model and Query builder for Laravel

7,998 10
audunru/eager-load-pivot-relations

Eager load pivot relations for Laravel Eloquent's BelongsToMany relation.

81,294 9
someonefamous/laravel-findby

Extended 'find' functionality for Eloquent (Laravel).

11,661 3
vluzrmos/eloquent-simple-searchable

An Eloquent Simple Searchable Scope.

216 2

Version History

Version Released PHP Laravel License
v2.2.5 >=5.6.4 ^5.4||^6||^7||^8 MIT
v2.2.4 >=5.6.4 ^5.4||^6||^7 MIT
v2.2.3 >=5.6.4 ^5.4||^6 MIT
v2.2.2 >=5.6.4 5.4.*|5.5.*|5.6.*|5.7.*|5.8.* MIT
v2.2.1 >=5.6.4 5.4.*|5.5.*|5.6.*|5.7.* MIT