jasperchou / laravel-sync-relations

Small package to sync one to many and polymorphic one to many data in Laravel Eloquent.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Laravel Sync Relations

Latest Version on Packagist Software License Build Status

Small package to sync one to many and polymorphic one to many data in Laravel Eloquent.

Install

Via Composer

$ composer require routegroup/laravel-sync-relations

Example

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.

License

The MIT License (MIT). Please see License File for more information.

About

Small package to sync one to many and polymorphic one to many data in Laravel Eloquent.

License:MIT License


Languages

Language:PHP 100.0%