Download the PHP package mateusjatenee/laravel-persist without Composer
On this page you can find all versions of the php package mateusjatenee/laravel-persist. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mateusjatenee/laravel-persist
More information about mateusjatenee/laravel-persist
Files in mateusjatenee/laravel-persist
Package laravel-persist
Short Description This is my package laravel-persist
License MIT
Homepage https://github.com/mateusjatenee/laravel-persist
Informations about the package laravel-persist
Persist
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:
- When you assign a property (e.g
$post->owner = $user
), the package checks whether that property is a relation, and if so, callssetRelation
to properly set it. - When you call the
persist
method, it works in a similar fashion topush
, 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
You can install the package via composer:
Usage
Simply add the Persist
trait to your models. For example:
Now you'll be able to persist the entire object graph using the persist
method. For example:
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
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
- Mateus Guimarães
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-persist with dependencies
illuminate/contracts Version ^10.0|^11.0
illuminate/database Version ^10.0|^11.0