PHPackages                             viktorruskai/advanced-upsert-for-laravel - 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. viktorruskai/advanced-upsert-for-laravel

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

viktorruskai/advanced-upsert-for-laravel
========================================

This package contains advanced upsert functionality for Laravel.

1.0.1(4y ago)25PHPPHP ^7.4

Since Mar 21Pushed 4y ago1 watchersCompare

[ Source](https://github.com/viktorruskai/advanced-upsert-for-laravel)[ Packagist](https://packagist.org/packages/viktorruskai/advanced-upsert-for-laravel)[ RSS](/packages/viktorruskai-advanced-upsert-for-laravel/feed)WikiDiscussions master Synced 3d ago

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

Advanced Upsert for Laravel
===========================

[](#advanced-upsert-for-laravel)

[![GitHub release (latest by date)](https://camo.githubusercontent.com/3bba40d4ede89a1065deaaf56562cb8b6aae661706a422e0fcdc4545b76f2353/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f76696b746f727275736b61692f616476616e6365642d7570736572742d666f722d6c61726176656c)](https://camo.githubusercontent.com/3bba40d4ede89a1065deaaf56562cb8b6aae661706a422e0fcdc4545b76f2353/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f76696b746f727275736b61692f616476616e6365642d7570736572742d666f722d6c61726176656c)[![PHPUnit](https://github.com/viktorruskai/advanced-upsert-for-laravel/actions/workflows/tests.yml/badge.svg)](https://github.com/viktorruskai/advanced-upsert-for-laravel/actions/workflows/tests.yml)[![PHPStan](https://github.com/viktorruskai/advanced-upsert-for-laravel/actions/workflows/phpstan.yml/badge.svg)](https://github.com/viktorruskai/advanced-upsert-for-laravel/actions/workflows/phpstan.yml)[![License](https://camo.githubusercontent.com/784362b26e4b3546254f1893e778ba64616e362bd6ac791991d2c9e880a3a64e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d677265656e2e737667)](https://github.com/viktorruskai/advanced-upsert-for-laravel/blob/master/LICENSE)

This feature is **for now only** available for PostgreSQL (in your `.env` file `DB_CONNECTION` must be set to `pgsql`). Upsert, based on [Wiktionary](https://en.wiktionary.org/wiki/upsert), is *an operation that inserts rows into a database table if they do not already exist, or updates them if they do*. The Advanced upsert is basically the same as Laravel's upsert function, but it has one key advantage. It can fetch foreign key (id) when performing an upsert.

⚡️️ Installation
----------------

[](#️️-installation)

```
$ composer require viktorruskai/advanced-upsert-for-laravel
```

⚙️ Usage
--------

[](#️-usage)

1. Add `use HasUpsert;` in your Laravel Eloquent model (make sure you have correct namespace)
2. You can use it in two ways:
    - **Normal upsert**```
        ItemAction::upsert(
            [
                [
                    'itemId' => 1,
                    'actionName' => 'Purchased',
                    'actionDescription' => 'Test description',
                    'actionValue' => 12,
                ],
                // ... (more items)
            ],
            ['itemId', 'actionName'], // Conflict (either columns or key name)
            ['actionDescription'] // Update column
        );
        ```

        *Generated SQL:*```
        INSERT INTO
            "itemActions" ("itemId", "actionName", "actionDescription", "actionValue", "updatedAt", "createdAt")
        VALUES
            (1, 'Purchased', 'Test description', 12, NOW(), NOW())
            /** ... (more items) */
        ON CONFLICT ("itemId", "actionName")
        DO UPDATE SET
            "actionDescription" = "excluded"."actionDescription"
        ```
    - **Upsert with selecting foreign ID from different table**```
        ItemActionAdditional::upsert(
            [
                [
                    'where' => [
                        'itemId' => 1,
                        'actionName' => 'Test',
                    ],
                    'upsert' => [
                        'itemActionId' => '*' // Must be set `*`, this ID will be automatically added from `$selectModelClassName` by conditions from `where` param
                        'specialData' => '123456',
                        'description' => 'Hello',
                    ],
                ],
                // ... (more items)
            ],
            ['itemActionId', 'specialData'], // Must be set as unique key (name of columns must be presented or name of the key)
            ['description'], // Columns that will be updated
            ItemAction::class, // Eloquent model, in this case must be set
            [...] // Any columns that should be returned (Not required)
        );
        ```

        *Generated SQL:*```
        INSERT INTO
            "itemActionAdditional" ("itemActionId", "specialData", "description", "updatedAt", "createdAt")
            (
                SELECT
                    id,
                    '123456',
                    'Hello',
                    NOW(),
                    NOW()
                FROM
                    "itemActions"
                WHERE
                    "itemId" = 1 AND
                    "actionName" = 'Test'
            )
            /** ... (more items) */
        ON CONFLICT ("itemActionId", "specialData")
        DO UPDATE SET
            "description" = "excluded"."description"
        ```

🌍 Examples
----------

[](#-examples)

Check the `tests/Support/Tests/` folder for more examples.

⚖️ Licence
----------

[](#️-licence)

Content of this package is open-sourced code licensed under the MIT license.

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~7 days

Total

2

Last Release

1505d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/273ebfe999362cb2c39be014e5e641833b8a0ab1158ac7cd3c613b2911f14638?d=identicon)[viktorruskai](/maintainers/viktorruskai)

---

Top Contributors

[![viktorruskai](https://avatars.githubusercontent.com/u/9396295?v=4)](https://github.com/viktorruskai "viktorruskai (158 commits)")

---

Tags

eloquentlaravelmodelupsert

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/viktorruskai-advanced-upsert-for-laravel/health.svg)

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

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