huboshen / laravel-one-to-many-sync
Allow sync method for Laravel Has Many and Morph Many Relationship.
Installs: 11 215
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 2
Forks: 0
Open Issues: 0
This package is auto-updated.
Last update: 2025-04-26 16:50:02 UTC
README
Allow sync method for Laravel HasMany and MorphMany Relationship.
Prerequisite
Laravel 5 or above should be good.
Installing
composer require huboshen/laravel-one-to-many-sync
After updating composer, add the service provider to the providers array in config/app.php
Huboshen\OneToManySync\OneToManySyncServiceProvider::class,
Laravel 5.5 (or above) uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider.
Usage
Synchronizing with a hasMany relationship in Laravel
When a post has many comments with a hasMany relationship
class Post extends Model { protected $fillable = ['title']; public $timestamps = true; /** * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function comments() { return $this->hasMany(Comment::class); } }
Synchronize the post with these comments with a "sync_one_to_many" method
$post->comments()->sync_one_to_many([ ['id' => 1, 'body' => 'update the comment where id = 1'], ['body' => 'this is a new comment without a id yet'], ]);
As a result, no matter how much comments the post originally have, the post would have only two comments left:
- comment with id is 1, with the "body" field updated as above.
- a new comment with "body" field is "this is a new comment without a id".
Don't want to delete things when synchronizing?
No problem. Just pass false as a second parameter to indicate whether enabling the deletion behavior (default is true).
$post->comments()->sync_one_to_many([ ['id' => 1, 'body' => 'update the comment where id = 1'], ['body' => 'this is a new comment without a id yet'], ], false);
Don't want to update the timestamp of the parent model when synchronizing?
No problem. Just pass false as a third parameter to indicate whether enabling a parent touch (default is true).
$post->comments()->sync_one_to_many([ ['id' => 1, 'body' => 'update the comment where id = 1'], ['body' => 'this is a new comment without a id yet'], ], true, false);
Synchronizing with a morphMany relationship in Laravel
When a post has many comments with a morphMany relationship
class PostPoly extends Model { protected $table = "posts_poly"; protected $fillable = ['title']; /** * Get all of the post's comments. */ public function comments() { return $this->morphMany('App\Models\CommentPoly', 'commentable'); } }
Synchronize the post with these comments with a "sync_one_to_many_morph" method
$post->comments()->sync_one_to_many_morph( [ ['id' => 1, 'body' => 'update the comment where id = 1'], ['body' => 'this is a new comment without a id yet'], ]);
The result will be similar to the previous hasMany example.
"sync_one_to_many_morph" method has the second parameter for controlling deletion and the third paramter for controlling parent touch as well.
License
This project is licensed under the MIT License - see the LICENSE.md file for details
Acknowledgments
- This package derives from alfa6661/laravel-hasmany-sync.