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.
}
}