PHPackages                             mateusjatenee/laravel-persist - 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. mateusjatenee/laravel-persist

ActiveLibrary

mateusjatenee/laravel-persist
=============================

This is my package laravel-persist

v1.0.0(1y ago)784.7k↓25%2[1 issues](https://github.com/mateusjatenee/persist/issues)[4 PRs](https://github.com/mateusjatenee/persist/pulls)MITPHPPHP ^8.2CI passing

Since Nov 22Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/mateusjatenee/persist)[ Packagist](https://packagist.org/packages/mateusjatenee/laravel-persist)[ Docs](https://github.com/mateusjatenee/laravel-persist)[ RSS](/packages/mateusjatenee-laravel-persist/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (9)Versions (9)Used By (0)

Persist
=======

[](#persist)

[![Latest Version on Packagist](https://camo.githubusercontent.com/cc83ca9a096bd63fa4c7632916253a860e2a7177edea04b7295c06588cf42ad7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d61746575736a6174656e65652f6c61726176656c2d706572736973742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mateusjatenee/laravel-persist)[![GitHub Tests Action Status](https://camo.githubusercontent.com/3ddf2838264737046297e8f12a1c812caf7c483e25ceba0c73529736dae3e552/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d61746575736a6174656e65652f6c61726176656c2d706572736973742f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/mateusjatenee/laravel-persist/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/540f634b79c72c4a8f28549ee06970f6c0bd8c90ab15e0ab210e1b31ee0d5a55/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d61746575736a6174656e65652f6c61726176656c2d706572736973742f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/mateusjatenee/laravel-persist/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/b5d6d84c3a23d524a782fe541d4b83f9db0386e7185a358d190a6e61d25ef8f3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d61746575736a6174656e65652f6c61726176656c2d706572736973742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mateusjatenee/laravel-persist)

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

[](#introduction)

The package offers an extension on top of Eloquent, enabling developers to handle the persistence of entire object graphs as a single unit of work. This package simplifies managing complex data models with multiple interrelated entities, ensuring a more efficient and reliable data handling process.

It works similarly to the native `push` method, but `push` **does not persist** any records for the first time. Therefore, you cannot build an object with its relationships and use `push` to save everything.

Persist works by hooking on two specific pieces of the lifecycle:

1. When you assign a property (e.g `$post->owner = $user`), the package checks whether that property is a relation, and if so, calls `setRelation` to properly set it.
2. When you call the `persist` method, it works in a similar fashion to `push`, but it adds hooks to persist the related objects *before or after* the base object. There are subtle differences on persistence order for different relationship types.

On top of that, Persist also runs the entire operation inside a database transaction. This means that if any part of the object graph fails to persist, the entire operation will be rolled back, maintaining database integrity.

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

[](#installation)

You can install the package via composer:

```
composer require mateusjatenee/laravel-persist
```

Usage
-----

[](#usage)

Simply add the `Persist` trait to your models. For example:

```
namespace App\Models;

class Order extends Model
{
    use Persist;
}
```

Now you'll be able to persist the entire object graph using the `persist` method. For example:

```
class ProcessCheckoutHandler
{
    public function __construct(
        private DatabaseManager $database,
    ) {
    }

    public function handle(ProcessCheckout $command)
    {
        $order = Order::startForCustomer($command->customer->id);
        $order->lines->push($command->cartItems->toOrderLines()); // Pushes an object to the "lines" relationship, which is a HasMany relation.

        $charge = $command->gateway->pay($command->pendingPayment);

        $order->payment = Payment::fromCharge($charge); // Sets the payment relationship, a "BelongsTo" relation.
        $order->payment->customer = $command->customer; // Sets the customer relationship "2-levels deep".

        $order->persist();

        return $order;
    }
}
```

In the example above, 4 entities will be persisted to the database: `Order`, `OrderLine`, `Payment`, and `Customer`.
`persist` runs, by default, within a transaction, so that if any queries fail, the entire transaction is rolled back.

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

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

[](#contributing)

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

Credits
-------

[](#credits)

- [Mateus Guimarães](https://github.com/mateusjatenee)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

48

—

FairBetter than 95% of packages

Maintenance70

Regular maintenance activity

Popularity36

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

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

Total

5

Last Release

424d ago

Major Versions

0.3.0 → v1.0.02025-03-20

PHP version history (2 changes)0.1.0PHP ^8.1

v1.0.0PHP ^8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/10816999?v=4)[Mateus Guimarães](/maintainers/mateusjatenee)[@mateusjatenee](https://github.com/mateusjatenee)

---

Top Contributors

[![mateusjatenee](https://avatars.githubusercontent.com/u/10816999?v=4)](https://github.com/mateusjatenee "mateusjatenee (29 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (6 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (4 commits)")

---

Tags

laravelMateus Guimarãeslaravel-persist

###  Code Quality

TestsPHPUnit

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/mateusjatenee-laravel-persist/health.svg)

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

###  Alternatives

[silber/bouncer

Eloquent roles and abilities.

3.6k4.4M25](/packages/silber-bouncer)[spatie/laravel-health

Monitor the health of a Laravel application

86910.0M83](/packages/spatie-laravel-health)[watson/validating

Eloquent model validating trait.

9723.3M47](/packages/watson-validating)[clickbar/laravel-magellan

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

423715.4k1](/packages/clickbar-laravel-magellan)[yadahan/laravel-authentication-log

Laravel Authentication Log provides authentication logger and notification for Laravel.

416632.8k5](/packages/yadahan-laravel-authentication-log)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)

PHPackages © 2026

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