PHPackages                             jdavidbakr/replaceable-model - 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. jdavidbakr/replaceable-model

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

jdavidbakr/replaceable-model
============================

Adds 'REPLACE' and 'INSERT IGNORE' query capability to eloquent models

1.4(4y ago)72245.5k↓25%11[1 PRs](https://github.com/jdavidbakr/replaceable-model/pulls)1MITPHPPHP &gt;=7.2.5

Since Apr 7Pushed 3y ago3 watchersCompare

[ Source](https://github.com/jdavidbakr/replaceable-model)[ Packagist](https://packagist.org/packages/jdavidbakr/replaceable-model)[ Docs](https://github.com/jdavidbakr/replaceable-model)[ RSS](/packages/jdavidbakr-replaceable-model/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (11)Used By (1)

ReplaceableModel
================

[](#replaceablemodel)

[![Latest Version on Packagist](https://camo.githubusercontent.com/6292b239adc90c84acfae54e0a6ada118f9d29976737def1c84ab31b6ba2ab01/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a646176696462616b722f7265706c61636561626c652d6d6f64656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jdavidbakr/replaceable-model)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Total Downloads](https://camo.githubusercontent.com/b4b388b037e2a3e89d2dcf536a914a5efacfe9d0aaad6951e774cf8e0722671a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a646176696462616b722f7265706c61636561626c652d6d6f64656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jdavidbakr/replaceable-model)[![Travis CI](https://camo.githubusercontent.com/f32c423ab45cd5357d74edefaa84fb8be53bda03f06f4c4bfca1b8773fe652e8/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6a646176696462616b722f7265706c61636561626c652d6d6f64656c2f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/jdavidbakr/replaceable-model)

The default Eloquent model is great for most cases, but if you have a database table that has additional constraints you may run into race conditions where the standard update() call will fail. Imagine, for example, the following table structure:

```
id auto increment
user_id
widget_id
date

```

where you have a constraint with each user can only have one widget per day, so you have a unique constraint across user\_id and date. Now in your interface you have a form that uses ajax calls to update the entries in this table, which may include removing items based on which items are selected. Because the form submits, say, an entire month's worth of widgets, you don't want to loop through and do individual inserts - you want to perform a single insert query. So you have something like the following in your php code:

```
Model::where('user_id',$user_id)->delete();
Model::insert($inserts);
```

This essentially performs the following under the hood:

```
delete from table where user_id = A;
insert into table (user_id, widget_id, date) values (A, B, C) ...
```

If you get stuck in a race condition, you might end up with the following:

```
delete from table where user_id = A; -- process #1
delete from table where user_id = A; -- process #2
insert into table (user_id, widget_id, date) values (A, B, C) ... -- process #1
insert into table (user_id, widget_id, date) values (A, B, C) ... -- process #2 - Exception!
```

The second insert will result in an exception. If the second query had an additional row than the first one, that insert is lost forever.

Before Laravel, I would normally have handled this type of situation with REPLACE or INSERT IGNORE commands. REPLACE will do a delete and insert based on any constraints in the query, and INSERT IGNORE will perform the insert but if there are any rows that cause constraint collisions those rows will not be updated. You would use REPLACE if you want the last query to overwrite any existing rows, and you would use INSERT IGNORE to only insert new rows.

Because this is a specific feature of MySQL, Laravel does not support it in Eloquent. The standard solution is to perform a raw query. This is ok, but it is kind of cumbersome to build this query every time with the bindings, etc, and I decided it would be helpful to create a trait for Eloquent that handles all of this for me and can be accessed in the same way that I would use insert().

Note that I'm **NOT** using the Builder class here. I'm directly extending the Model class and as such you won't be able to chain these functions like you might the regular insert() command. This is really just a macro to fix a problem that I had. I welcome any pull requests that solve additional problems you may have with this package.

The 'saving' and 'saved' events **are** fired for both of these commands.

Install
-------

[](#install)

Via Composer

```
$ composer require jdavidbakr/replaceable-model
```

Usage
-----

[](#usage)

Apply the trait to your models to activate the ability to use replace and insertIgnore

```
class model extends Model {
	...
	use \jdavidbakr\ReplaceableModel\ReplaceableModel
	...
}
```

Then build your insert array like you would for the insert() call and call one of the two functions:

```
$inserts = [...];
\App\Model::replace($inserts);
\App\Model::insertIgnore($inserts);
```

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

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [J David Baker](https://github.com/jdavidbakr)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity46

Moderate usage in the ecosystem

Community20

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

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

Recently: every ~226 days

Total

10

Last Release

1518d ago

PHP version history (2 changes)1.0PHP ~5.5|~7.0

1.2PHP &gt;=7.2.5

### Community

Maintainers

![](https://www.gravatar.com/avatar/28fc80e04726581ef8fd2cda5300c45a22d67437c62bbaa7be372feb46256640?d=identicon)[jdavidbakr](/maintainers/jdavidbakr)

---

Top Contributors

[![jdavidbakr](https://avatars.githubusercontent.com/u/25177?v=4)](https://github.com/jdavidbakr "jdavidbakr (12 commits)")[![jonb-sf](https://avatars.githubusercontent.com/u/178495300?v=4)](https://github.com/jonb-sf "jonb-sf (1 commits)")[![kirill001](https://avatars.githubusercontent.com/u/8287881?v=4)](https://github.com/kirill001 "kirill001 (1 commits)")[![michaeldyrynda](https://avatars.githubusercontent.com/u/558441?v=4)](https://github.com/michaeldyrynda "michaeldyrynda (1 commits)")[![mseymour](https://avatars.githubusercontent.com/u/188480?v=4)](https://github.com/mseymour "mseymour (1 commits)")

---

Tags

jdavidbakrreplaceable-model

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jdavidbakr-replaceable-model/health.svg)

```
[![Health](https://phpackages.com/badges/jdavidbakr-replaceable-model/health.svg)](https://phpackages.com/packages/jdavidbakr-replaceable-model)
```

###  Alternatives

[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[plank/laravel-mediable

A package for easily uploading and attaching media files to models with Laravel

8271.5M11](/packages/plank-laravel-mediable)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

423715.4k1](/packages/clickbar-laravel-magellan)[genealabs/laravel-pivot-events

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

1404.9M8](/packages/genealabs-laravel-pivot-events)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)[aglipanci/laravel-eloquent-case

Adds CASE statement support to Laravel Query Builder.

115157.2k](/packages/aglipanci-laravel-eloquent-case)

PHPackages © 2026

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