PHPackages                             iamirnet/laravel-upsert - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. iamirnet/laravel-upsert

ActiveLibrary[Database &amp; ORM](/categories/database)

iamirnet/laravel-upsert
=======================

Laravel UPSERT and INSERT IGNORE queries, Upgrade Laravel 8x,9x,10x,11x

03PHP

Since May 24Pushed 1y agoCompare

[ Source](https://github.com/iamirnet/laravel-upsert)[ Packagist](https://packagist.org/packages/iamirnet/laravel-upsert)[ RSS](/packages/iamirnet-laravel-upsert/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

[![Code Coverage](https://camo.githubusercontent.com/9d50f0582972626d666d392a015a39a2739fe1ac55167b554593c90e27cf1cc0/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f69616d69726e65742f6c61726176656c2d7570736572742f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/iamirnet/laravel-upsert/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/ba2ac94a9875755ecf0c5aebf2927ad397648b9f2eb52c3e8009c83a75d52805/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f69616d69726e65742f6c61726176656c2d7570736572742f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/iamirnet/laravel-upsert/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/dda92d9472651f6b8c8e1d7eeba8df448fd3340b69813a78ba5c7c481680ac99/68747470733a2f2f706f7365722e707567782e6f72672f69616d69726e65742f6c61726176656c2d7570736572742f762f737461626c65)](https://packagist.org/packages/iamirnet/laravel-upsert)[![Total Downloads](https://camo.githubusercontent.com/acd80cbd8f396374c4440eed49e28cd8e45f2ba65e343a4f3fbd411171150153/68747470733a2f2f706f7365722e707567782e6f72672f69616d69726e65742f6c61726176656c2d7570736572742f646f776e6c6f616473)](https://packagist.org/packages/iamirnet/laravel-upsert)[![License](https://camo.githubusercontent.com/b42647cc1325f47dc526cc93d63d5a517a600bc4bc5934cfe91ba31cc68cc3c7/68747470733a2f2f706f7365722e707567782e6f72672f69616d69726e65742f6c61726176656c2d7570736572742f6c6963656e7365)](https://packagist.org/packages/iamirnet/laravel-upsert)

Important

The package's code has been merged into Laravel 8.10+ and UPSERT queries are now supported natively.

Introduction
------------

[](#introduction)

This Laravel extension adds support for INSERT &amp; UPDATE (UPSERT) and INSERT IGNORE to the query builder and Eloquent.

Supports Laravel 5.5,8.x,9.x,10.x,11x.

Compatibility
-------------

[](#compatibility)

- MySQL 5.1+: [INSERT ON DUPLICATE KEY UPDATE](https://dev.mysql.com/doc/refman/en/insert-on-duplicate.html)
- MariaDB 5.1+: [INSERT ON DUPLICATE KEY UPDATE](https://mariadb.com/kb/en/library/insert-on-duplicate-key-update/)
- PostgreSQL 9.5+: [INSERT ON CONFLICT](https://www.postgresql.org/docs/current/sql-insert.html#SQL-ON-CONFLICT)
- SQLite 3.24.0+: [INSERT ON CONFLICT](https://www.sqlite.org/lang_UPSERT.html)
- SQL Server 2008+: [MERGE](https://docs.microsoft.com/sql/t-sql/statements/merge-transact-sql)

Installation
------------

[](#installation)

```
composer require iamirnet/laravel-upsert

```

Usage
-----

[](#usage)

- [INSERT &amp; UPDATE (UPSERT)](#insert--update-upsert)
- [INSERT IGNORE](#insert-ignore)
- [Eloquent](#eloquent)
- [Lumen](#lumen)

### INSERT &amp; UPDATE (UPSERT)

[](#insert--update-upsert)

Consider this `users` table with a unique `username` column:

```
Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('username')->unique();
    $table->boolean('active');
    $table->timestamps();
});
```

Use `upsert()` to insert a new user or update the existing one. In this example, an inactive user will be reactivated and the `updated_at` timestamp will be updated:

```
DB::table('users')->upsert(
    ['username' => 'foo', 'active' => true, 'created_at' => now(), 'updated_at' => now()],
    'username',
    ['active', 'updated_at']
);
```

Provide the values to be inserted as the first argument. This can be a single record or multiple records.

The second argument is the column(s) that uniquely identify records. All databases except SQL Server require these columns to have a `PRIMARY` or `UNIQUE` index.

Provide the columns to be the updated as the third argument (optional). By default, all columns will be updated. You can provide column names and key-value pairs with literals or raw expressions (see below).

As an example with a composite key and a raw expression, consider this table that counts visitors per post and day:

```
Schema::create('stats', function (Blueprint $table) {
    $table->unsignedInteger('post_id');
    $table->date('date');
    $table->unsignedInteger('views');
    $table->primary(['post_id', 'date']);
});
```

Use `upsert()` to log visits. The query will create a new record per post and day or increment the existing view counter:

```
DB::table('stats')->upsert(
    [
        ['post_id' => 1, 'date' => now()->toDateString(), 'views' => 1],
        ['post_id' => 2, 'date' => now()->toDateString(), 'views' => 1],
    ],
    ['post_id', 'date'],
    ['views' => DB::raw('stats.views + 1')]
);
```

### INSERT IGNORE

[](#insert-ignore)

You can also insert records while ignoring duplicate-key errors:

```
Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('username')->unique();
    $table->timestamps();
});

DB::table('users')->insertIgnore([
    ['username' => 'foo', 'created_at' => now(), 'updated_at' => now()],
    ['username' => 'bar', 'created_at' => now(), 'updated_at' => now()],
]);
```

SQL Server requires a second argument with the column(s) that uniquely identify records:

```
DB::table('users')->insertIgnore(
    ['username' => 'foo', 'created_at' => now(), 'updated_at' => now()],
    'username'
);
```

### Eloquent

[](#eloquent)

You can use UPSERT and INSERT IGNORE queries with Eloquent models.

In Laravel 5.5–5.7, this requires the `HasUpsertQueries` trait:

```
class User extends Model
{
    use \iamirnet\LaravelUpsert\Eloquent\HasUpsertQueries;
}

User::upsert(['username' => 'foo', 'active' => true], 'username', ['active']);

User::insertIgnore(['username' => 'foo']);
```

If the model uses timestamps, `upsert()` and `insertIgnore()` will automatically add timestamps to the inserted values. `upsert()` will also add `updated_at` to the updated columns.

### Lumen

[](#lumen)

If you are using Lumen, you have to instantiate the query builder manually:

```
$builder = new \iamirnet\LaravelUpsert\Query\Builder(app('db')->connection());

$builder->from(...)->upsert(...);
```

In Eloquent, the `HasUpsertQueries` trait is required for *all* versions of Lumen.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) and [CODE OF CONDUCT](.github/CODE_OF_CONDUCT.md) for details.

###  Health Score

14

—

LowBetter than 2% of packages

Maintenance26

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity18

Early-stage or recently created project

 Bus Factor1

Top contributor holds 92.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/8c9bcb49a108c207db8d27ae8144c98f9629aee3cde4f86407f204dda0ee08d6?d=identicon)[jahani](/maintainers/jahani)

---

Top Contributors

[![staudenmeir](https://avatars.githubusercontent.com/u/1853169?v=4)](https://github.com/staudenmeir "staudenmeir (25 commits)")[![iamirnet](https://avatars.githubusercontent.com/u/68027783?v=4)](https://github.com/iamirnet "iamirnet (2 commits)")

### Embed Badge

![Health badge](/badges/iamirnet-laravel-upsert/health.svg)

```
[![Health](https://phpackages.com/badges/iamirnet-laravel-upsert/health.svg)](https://phpackages.com/packages/iamirnet-laravel-upsert)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
