Download the PHP package jwohlfert23/laravel-sync-relations without Composer
On this page you can find all versions of the php package jwohlfert23/laravel-sync-relations. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jwohlfert23/laravel-sync-relations
More information about jwohlfert23/laravel-sync-relations
Files in jwohlfert23/laravel-sync-relations
Package laravel-sync-relations
Short Description Package to sync nested relations from request data
License MIT
Informations about the package laravel-sync-relations
Laravel Sync Relations
Add this to your models:
And you can use it in your controllers like so:
Let's assume the following
- Post model has a "hasMany" relationship with tags.
- The request data looks like:
By running saveAndSync
, it will iterate over the 2 tags provided in the request. Because the first one already exists, it will update that one and set the name to "My First Tag". The second one doesn't exist aleady so it will create it using the name "My Second Tag". After this controller processeed this request, you could run the following:
and get this:
This package also works with "belongsToMany" and "belongsTo" relationship types. However, for those, it will not update/create the nested objects. It will only associate the existing models with the parent model (in other words, for these relationship, you must provide the primary key for each child).
Additionally, you could run saveAndSync(request()->input(), ['comments.author'])
to do a nested sync.
All versions of laravel-sync-relations with dependencies
nesbot/carbon Version ~2
illuminate/database Version ^10.0|^11.0
illuminate/support Version ^10.0|^11.0
illuminate/validation Version ^10.0|^11.0