2017 © Pedro Peláez
 

library laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

image

fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  • Saturday, July 28, 2018
  • by fico7489
  • Repository
  • 10 Watchers
  • 113 Stars
  • 48,437 Installations
  • PHP
  • 8 Dependents
  • 1 Suggesters
  • 15 Forks
  • 0 Open issues
  • 40 Versions
  • 81 % Grown

The README.md

Laravel Pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation., (*1)

Laravel Problems

In Laravel events are not dispatched when BelongsToMany relation (pivot table) is updated with sync(), attach(), detach() or updateExistingPivot() methods, but this package will help with that., (*2)

Version Compatibility

Laravel Version Package Tag Supported Development Branch
>= 5.5.0 3.* yes master
< 5.5.0 - no -
  • you still can use inactive branches for laravel 5.4.x or older

Install

1.Install package with composer, (*3)

composer require fico7489/laravel-pivot

With this statement, a composer will install highest available package version for your current laravel version., (*4)

2.Use Fico7489\Laravel\Pivot\Traits\PivotEventTrait trait in your base model or only in particular models., (*5)

use Fico7489\Laravel\Pivot\Traits\PivotEventTrait;
use Illuminate\Database\Eloquent\Model;

abstract class BaseModel extends Model
{
    use PivotEventTrait;
...

and that's it, enjoy., (*6)

New eloquent events

You can check all eloquent events here: https://laravel.com/docs/5.5/eloquent#events), (*7)

New events are :, (*8)

pivotAttaching, pivotAttached
pivotDetaching, pivotDetached,
pivotUpdating, pivotUpdated

The best way to catch events is with this model functions:, (*9)

public static function boot()
{
    parent::boot();

    static::pivotAttaching(function ($model, $relationName, $pivotIds, $pivotIdsAttributes) {
        //
    });

    static::pivotAttached(function ($model, $relationName, $pivotIds, $pivotIdsAttributes) {
        //
    });

    static::pivotDetaching(function ($model, $relationName, $pivotIds) {
        //
    });

    static::pivotDetached(function ($model, $relationName, $pivotIds) {
        //
    });

    static::pivotUpdating(function ($model, $relationName, $pivotIds, $pivotIdsAttributes) {
        //
    });

    static::pivotUpdated(function ($model, $relationName, $pivotIds, $pivotIdsAttributes) {
        //
    });

    static::updating(function ($model) {
        //this is how we catch standard eloquent events
    });
}

You can also see those events here:, (*10)

\Event::listen('eloquent.*', function ($eventName, array $data) {
    echo $eventName;  //e.g. 'eloquent.pivotAttached'
});

Suported relations

BelongsToMany and MorphToMany, (*11)

Which events are dispatched and when they are dispatched

Four BelongsToMany methods dispatches events from this package:, (*12)

attach()
Dispatches one pivotAttaching and one pivotAttached event.
Even when more rows are added only one event is dispatched for all rows but in that case, you can see all changed row ids in the $pivotIds variable, and the changed row ids with attributes in the $pivotIdsAttributes variable., (*13)

detach()
Dispatches one pivotDetaching and one pivotDetached event.
Even when more rows are deleted only one event is dispatched for all rows but in that case, you can see all changed row ids in the $pivotIds variable., (*14)

updateExistingPivot()
Dispatches one pivotUpdating and one pivotUpdated event.
You can change only one row in the pivot table with updateExistingPivot., (*15)

sync()
Dispatches more pivotAttaching and more pivotAttached events, depending on how many rows are added in the pivot table. These events are not dispatched if nothing is attached.
Dispatches one pivotDetaching and one pivotDetached event, but you can see all deleted ids in the $pivotIds variable. This event is not dispatched if nothing is detached.
Dispatches more pivotUpdating and more pivotUpdated events, depending on how many rows are updated in the pivot table. These events are not dispatched if nothing is attached., (*16)

E.g. when you call sync() if two rows are added and two are deleted two pivotAttaching and two pivotAttached events and one pivotDetaching and one pivotDetached event will be dispatched.
If sync() is called but rows are not added or deleted events are not dispatched., (*17)

Usage

We have three tables in database users(id, name), roles(id, name), role_user(user_id, role_id). We have two models :, (*18)


class User extends Model { use PivotEventTrait; .... public function roles() { return $this->belongsToMany(Role::class); } static::pivotAttached(function ($model, $relationName, $pivotIds, $pivotIdsAttributes) { echo 'pivotAttached'; echo get_class($model); echo $relationName; print_r($pivotIds); print_r($pivotIdsAttributes); }); static::pivotUpdated(function ($model, $relationName, $pivotIds, $pivotIdsAttributes) { echo 'pivotUpdated'; echo get_class($model); echo $relationName; print_r($pivotIds); print_r($pivotIdsAttributes); }); static::pivotDetached(function ($model, $relationName, $pivotIds) { echo 'pivotDetached'; echo get_class($model); echo $relationName; print_r($pivotIds); });
class Role extends Model
{
    ....

Attaching

For attach() or detach() one event is dispatched for both pivot ids., (*19)

Attaching with int

Running this code, (*20)

$user = User::first();
$user->roles()->attach(1);

You will see this output, (*21)

pivotAttached
App\Models\User
roles
[1]
[1 => []]

Attaching with array

Running this code, (*22)

$user = User::first();
$user->roles()->attach([1]);

You will see this output, (*23)

pivotAttached
App\Models\User
roles
[1]
[1 => []]

Attaching with model

Running this code, (*24)

$user = User::first();
$user->roles()->attach(Role::first());

You will see this output, (*25)

pivotAttached
App\Models\User
roles
[1]
[1 => []]

Attaching with collection

Running this code, (*26)

$user = User::first();
$user->roles()->attach(Role::get());

You will see this output, (*27)

pivotAttached
App\Models\User
roles
[1, 2]
[1 => [], 2 => []]

Attaching with array (id => attributes)

Running this code, (*28)

$user = User::first();
$user->roles()->attach([1, 2 => ['attribute' => 'test']], ['attribute2' => 'test2']);

You will see this output, (*29)

pivotAttached
App\Models\User
roles
[1, 2]
[1 => [], 2 => ['attribute' => 'test', 'attribute2' => 'test2']]

Syncing:

For sync() method event is dispatched for each pivot row., (*30)

Running this code, (*31)

$user = User::first();
$user->roles()->sync([1, 2]);

You will see this output, (*32)

pivotAttached
App\Models\User
roles
[1]
[1 => []]

pivotAttached
App\Models\User
roles
[2]
[2 => []]

Detaching:

Running this code, (*33)

$user = User::first();
$user->roles()->detach([1, 2]);

You will see this output, (*34)

pivotDetached
App\Models\User
roles
[1, 2]

Updating:

Running this code, (*35)

$user = User::first();
$user->roles()->updateExistingPivot(1, ['attribute' => 'test']);

You will see this output, (*36)

pivotUpdated
App\Models\User
roles
[1]
[1 => ['attribute' => 'test']]

License

MIT, (*37)

Free Software, Hell Yeah!, (*38)

The Versions

28/07 2018

2.1.x-dev

2.1.9999999.9999999-dev https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

28/07 2018

2.1.7

2.1.7.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

28/07 2018

dev-master

9999999-dev https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel laravel pivot laravel belongstomany laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

28/07 2018

2.2.7

2.2.7.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

08/03 2018

1.2.x-dev

1.2.9999999.9999999-dev https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel laravel pivot laravel belongstomany laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

08/03 2018

1.2.6

1.2.6.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

08/03 2018

1.3.x-dev

1.3.9999999.9999999-dev https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel laravel pivot laravel belongstomany laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

08/03 2018

1.3.6

1.3.6.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

08/03 2018

2.0.x-dev

2.0.9999999.9999999-dev https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel laravel pivot laravel belongstomany laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

08/03 2018

2.0.6

2.0.6.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

08/03 2018

2.1.6

2.1.6.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

08/03 2018

2.2.6

2.2.6.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

22/02 2018

2.0.5

2.0.5.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

22/02 2018

2.2.5

2.2.5.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

22/02 2018

2.1.5

2.1.5.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

12/02 2018

2.2.4

2.2.4.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

06/01 2018

1.2.4

1.2.4.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

06/01 2018

1.3.4

1.3.4.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

06/01 2018

2.0.4

2.0.4.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

14/12 2017

2.1.4

2.1.4.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

10/12 2017

1.2.3

1.2.3.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

10/12 2017

1.3.3

1.3.3.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

10/12 2017

2.0.3

2.0.3.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

10/12 2017

2.1.3

2.1.3.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

09/12 2017

1.2.2

1.2.2.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

09/12 2017

1.3.2

1.3.2.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

09/12 2017

2.0.2

2.0.2.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

09/12 2017

2.1.2

2.1.2.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

08/12 2017

1.2.1

1.2.1.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

08/12 2017

1.3.1

1.3.1.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

08/12 2017

2.0.1

2.0.1.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

08/12 2017

2.1.1

2.1.1.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

08/12 2017

dev-develop

dev-develop https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for sync(), attach(), detach() or updateExistingPivot() methods on BelongsToMany relation.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel pivot events laravel belongstomany events eloquent extra events laravel sync events eloquent events

08/11 2017

2.1.0

2.1.0.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for changes on pivot table.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel laravel pivot laravel belongstomany laravel pivot events laravel belongstomany events

08/11 2017

2.0.0

2.0.0.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for changes on pivot table.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel laravel pivot laravel belongstomany laravel pivot events laravel belongstomany events

07/11 2017

1.3.0

1.3.0.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for changes on pivot table.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel laravel pivot laravel belongstomany

07/11 2017

1.0.x-dev

1.0.9999999.9999999-dev https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for changes on pivot table.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel laravel pivot laravel belongstomany

07/11 2017

1.2.0

1.2.0.0 https://github.com/fico7489/laravel-pivot

This package introduces new eloquent events for changes on pivot table.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel laravel pivot laravel belongstomany

01/11 2017

1.0.1

1.0.1.0 https://github.com/fico7489/LaravelPivot

Laravel updating|updated events for sync, attach, detach and updateExistingPivot.

  Sources   Download

MIT

The Requires

 

laravel laravel pivot laravel belongstomany

01/11 2017

1.0.0

1.0.0.0 https://github.com/fico7489/LaravelPivot

Laravel fire updating|updated events on base model when BelongsToMany methods are being called. Covers sync(), attach(), detach() and updateExistingPivot() functions.

  Sources   Download

MIT

The Requires

 

laravel laravel pivot laravel belongstomany