Skip to content

alfa6661/laravel-hasmany-sync

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel HasMany Sync

Allow sync method for Laravel Has Many Relationship.

Installation

You can install the package via composer:

composer require alfa6661/laravel-has-many-sync

Register the ServiceProvider in config/app.php

'providers' => [
    // ...
    Alfa6661\EloquentHasManySync\ServiceProvider::class,
],

Usage

Setup HasMany Relation

class Customer extends Model
{
    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function contacts()
    {
        return $this->hasMany(CustomerContact::class);
    }
}

You can access the sync method like this:

$customer->contacts()->sync([
    [
        'id' => 1,
        'name' => 'Alfa',
        'phone_number' => '123',
    ],
    [
        'id' => null,
        'name' => 'Adhitya',
        'phone_number' => '234,
    ]
]);

The sync method accepts an array of data to place on the intermediate table. Any data that are not in the given array will be removed from the intermediate table. So, after this operation is complete, only the data in the given array will exist in the intermediate table:

Syncing without deleting

If you do not want to delete existing data, you may pass false value to the second parameter in the sync method.

$customer->contacts()->sync([
    [
        'id' => 1,
        'name' => 'Alfa',
        'phone_number' => '123',
    ],
    [
        'id' => null,
        'name' => 'Adhitya',
        'phone_number' => '234,
    ]
], false);

Example usage in the controller.

class CustomersController extends Controller
{
    /**
     * Update the specified resource in storage.
     *
     * @param  CustomerRequest  $request
     * @param  Customer $customer
     * @return \Illuminate\Http\Response
     */
    public function update(CustomerRequest $request, Customer $customer)
    {
        DB::transaction(function () use ($customer, $request) {
            $customer->update($request->all());
            $customer->contacts()->sync($request->get('contacts', []));
        });

        return redirect()->route('customers.index');
    }
}

About

Allow sync method for Laravel Has Many Relationship.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages