PHPackages                             pristavu/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. pristavu/laravel-upsert

ActiveLibrary

pristavu/laravel-upsert
=======================

Laravel UPSERT and INSERT IGNORE queries

v2.0(3y ago)064MITPHPPHP ^8.1

Since Jan 29Pushed 3y agoCompare

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

READMEChangelog (2)Dependencies (3)Versions (3)Used By (0)

[![CI](https://github.com/staudenmeir/laravel-upsert/workflows/CI/badge.svg)](https://github.com/staudenmeir/laravel-upsert/workflows/CI/badge.svg)[![Code Coverage](https://camo.githubusercontent.com/3a9d1990bc93cab4ff346c2501bd2b81b27232a46915f45696b7b8f7023aa8b3/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7374617564656e6d6569722f6c61726176656c2d7570736572742f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/staudenmeir/laravel-upsert/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/dc344b02c1439a56f73304a865c7f0cad93dea1645a3c5d66ce98637810c4ee7/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7374617564656e6d6569722f6c61726176656c2d7570736572742f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/staudenmeir/laravel-upsert/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/e4e4ee9a6fc3575d4ed63e2b49eabfc20b54cacadb4e009b29eb9522114e07dc/68747470733a2f2f706f7365722e707567782e6f72672f7374617564656e6d6569722f6c61726176656c2d7570736572742f762f737461626c65)](https://packagist.org/packages/staudenmeir/laravel-upsert)[![Total Downloads](https://camo.githubusercontent.com/8916983c4b52c6aff5ba001677edf71e7aefe69dce6b2fccf24cdec67ad49e96/68747470733a2f2f706f7365722e707567782e6f72672f7374617564656e6d6569722f6c61726176656c2d7570736572742f646f776e6c6f616473)](https://packagist.org/packages/staudenmeir/laravel-upsert)[![License](https://camo.githubusercontent.com/f75539bc035ca2369bc7dd1371a5c87b47d48ccfbe77e050ee80451972d7b8f4/68747470733a2f2f706f7365722e707567782e6f72672f7374617564656e6d6569722f6c61726176656c2d7570736572742f6c6963656e7365)](https://packagist.org/packages/staudenmeir/laravel-upsert)

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

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 staudenmeir/laravel-upsert:"^1.0"

```

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 \Staudenmeir\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 \Staudenmeir\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

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 88% 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.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

1199d ago

Major Versions

v1.0 → v2.02023-01-29

### Community

Maintainers

![](https://www.gravatar.com/avatar/2875b9ef72b09212f72a878c4eb30c237ad1adf460ddd1a1f376ef7d4090f83e?d=identicon)[pristavu](/maintainers/pristavu)

---

Top Contributors

[![staudenmeir](https://avatars.githubusercontent.com/u/1853169?v=4)](https://github.com/staudenmeir "staudenmeir (22 commits)")[![pristavu](https://avatars.githubusercontent.com/u/4123729?v=4)](https://github.com/pristavu "pristavu (3 commits)")

### Embed Badge

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

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

###  Alternatives

[larastan/larastan

Larastan - Discover bugs in your code without running it. A phpstan/phpstan extension for Laravel

6.4k43.5M5.2k](/packages/larastan-larastan)[owen-it/laravel-auditing

Audit changes of your Eloquent models in Laravel

3.4k33.0M95](/packages/owen-it-laravel-auditing)[fumeapp/modeltyper

Generate TypeScript interfaces from Laravel Models

196277.9k](/packages/fumeapp-modeltyper)[casbin/laravel-authz

An authorization library that supports access control models like ACL, RBAC, ABAC in Laravel.

324339.9k4](/packages/casbin-laravel-authz)[pressbooks/pressbooks

Pressbooks is an open source book publishing tool built on a WordPress multisite platform. Pressbooks outputs books in multiple formats, including PDF, EPUB, web, and a variety of XML flavours, using a theming/templating system, driven by CSS.

44643.1k1](/packages/pressbooks-pressbooks)[illuminatech/balance

Provides support for Balance accounting system based on debit and credit principle

16137.4k](/packages/illuminatech-balance)

PHPackages © 2026

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