PHP code example of cheesegrits / laravel-pivot-events

1. Go to this page and download the library: Download cheesegrits/laravel-pivot-events library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cheesegrits / laravel-pivot-events example snippets


use Cheesegrits\Laravel\PivotEvents\Traits\PivotEventTrait;
use Illuminate\Database\Eloquent\Model;

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

use App\Models\YourModel;

class YourModelObserver
{
    public function saving(User $user)
    {
        //this is how we catch pivot events
    }
    
    public function pivotAttaching(function (YourModel $model, string $relationName, array $pivotIds, array $pivotIdsAttributes) {
        //
    });
    
    public function pivotAttached(function (YourModel $model, string $relationName, string $pivotIds, string $pivotIdsAttributes) {
        //
    });
    
    public function pivotDetaching(function (YourModel $model, string $relationName, string $pivotIds) {
        //
    });

    public function pivotDetached(function (YourModel $model, string $relationName, string $pivotIds) {
        //
    });
    
    public function pivotUpdating(function (YourModel $model, string $relationName, string $pivotIds, string $pivotIdsAttributes) {
        //
    });
    
    public function pivotUpdated(function (YourModel $model, string $relationName, string $pivotIds, string $pivotIdsAttributes) {
        //
    });
    
    public function updating(function (YourModel $model) {
        //this is how we catch standard eloquent events
    });
}

use Cheesegrits\Laravel\PivotEvents\Traits\PivotEventTrait;
use Illuminate\Database\Eloquent\Model;

class YourModel extends Model
{
    use PivotEventTrait;
    
    public static function boot()
    {
        parent::boot();
    
        static::pivotAttaching(function (YourModel $model, string $relationName, array $pivotIds, array $pivotIdsAttributes) {
            //
        });
        
        // etc.
    }
}

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


class User extends Model
{
    use PivotEventTrait;
    // ...
    
    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }
    
    public static function boot()
    {
        parent::boot();
        
        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
{
    // ...
}

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

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

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

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

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

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

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

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