Download the PHP package denizgolbas/eloquent-save-together without Composer
On this page you can find all versions of the php package denizgolbas/eloquent-save-together. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download denizgolbas/eloquent-save-together
More information about denizgolbas/eloquent-save-together
Files in denizgolbas/eloquent-save-together
Package eloquent-save-together
Short Description For Laravel Eloquent that makes it easy to save related models together.
License MIT
Homepage https://github.com/denizgolbas/eloquent-save-together
Informations about the package eloquent-save-together
Eloquent SaveTogether
A powerful Laravel package that allows you to save Eloquent models along with all their relationships in a single operation. Perfect for handling complex nested data structures from API requests or forms.
Features
- 🚀 Save parent and all related models with one method call
- 🔄 Automatic handling of create/update operations based on ID presence
- 🗑️ Smart deletion of removed related records
- 🌳 Support for deeply nested relationships
- ⚡ Clean and intuitive API
- 🎯 Type-safe with full IDE support
Installation
You can install the package via composer:
Optionally, you can publish the config file to define custom relation mappings:
Basic Usage
1. Add the trait to your model
2. Handle the request data
Example Request Data
Understanding the $together Property
The $together array property defines which relationships should be handled:
Boolean Values (Delete Control)
true: Full sync mode - Creates, updates AND deletes records not present in requestfalse: Partial sync mode - Only creates and updates, preserves existing records not in request
Example Scenarios
Scenario 1: Full Sync (true)
Scenario 2: Partial Sync (false)
Advanced Usage
Nested Relationships
The package supports deeply nested relationships. If a related model also uses the EloquentSaveTogether trait, its relationships will be saved recursively:
Custom Relation Mappings
If you're using custom relation classes, you can map them in the config:
Get Fillable Fields with Relations
To get all fillable fields including nested relations:
Supported Relationship Types
One-to-One Relations
hasOne()belongsTo()morphTo()morphOne()
One-to-Many Relations
hasMany()morphMany()
Many-to-Many Relations
belongsToMany()morphToMany()
Important Notes
-
Snake Case Convention: Relationship names in request data should use snake_case
-
ID Field: Include
idfield in request to update existing records- First item with
id: 1updates existing record - Second item without
idcreates new record
- First item with
-
Validation: Always validate your request data before using
fillTogether() - Mass Assignment: Ensure related models have proper
$fillableproperties defined
Credits
- denizgolbas
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of eloquent-save-together with dependencies
illuminate/database Version ^10.0|^11.0
illuminate/support Version ^10.0|^11.0
laravel/framework Version ^10.0|^11.0
nesbot/carbon Version ^2.67|^3.0