Small package to sync one to many and polymorphic one to many data in Laravel Eloquent.
Via Composer
$ composer require routegroup/laravel-sync-relations
In model
public function setOptionsAttribute($items)
{
if (!$this->exists) {
return ;
}
// this->options() is method which returns \Illuminate\Database\Eloquent\Relations\HasMany
return new SyncOneToMany($items, $this->options(), [
'page_id' => $this->id
]);
}
In ex. controller
public function create()
{
$page = Page::create([...]);
$page->options = [
['name' => 'Option 1'],
['name' => 'Option 2'],
['name' => 'Option 3']
];
}
Options can be as json encoded data, laravel collection or just array. For more examples look at tests.
The MIT License (MIT). Please see License File for more information.