jwohlfert23/laravel-sync-relations

Package to sync nested relations from request data
7,351
Install
composer require jwohlfert23/laravel-sync-relations
Latest Version:5.0.0
PHP:>=8.2
License:MIT
Last Updated:Mar 19, 2024
Links: GitHub  ·  Packagist
Maintainer: jwohlfert23

Laravel Sync Relations

Add this to your models:

use Jwohlfert23/LaravelSyncRelations/SyncableTrait;
...

class Post extends Model {
    use SyncableTrait;
}

And you can use it in your controllers like so:


public function update($id) {
    $post = Post::find($id);
    
    $post->saveAndSync(request()->input(), ['tags']);
}

Let's assume the following

  • Post model has a "hasMany" relationship with tags.
  • The request data looks like:
{
  "title": "My Post Title",
  "tags": [{
    "id": 1,
    "name": "My First Tag"
  }, {
     "name": "My Second Tag"
  }]
}

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:

Post::find($id)->tags->pluck('name')

and get this:

["My First Tag", "My Second Tag"]

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.

Related Packages

fish/eloquent-cascade

Cascading delete trait for Laravel's Eloquent

2,059 3
codedge/hookable

Laravel Eloquent hooks system - improved.

192 2
princealikhan/laravel-mautic-api

Free and Open Source Marketing Automation API

5,892 38