承接 mikebronner/laravel-pivot-events 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mikebronner/laravel-pivot-events

Composer 安装命令:

composer require mikebronner/laravel-pivot-events

包简介

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

README 文档

README

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

This package is a fork of fico7489/laravel-pivot created mainly to address compatibility issues with Laravel Telescope and Model Caching for Laravel.

Sponsors

We thank the following sponsors for their generosity. Please take a moment to check them out:

Requirements

  • Laravel 11.0+
  • PHP 8.2+

Installation

1.Install package with composer: composer require "mikebronner/laravel-pivot-events:*"

  1. Use GeneaLabs\LaravelPivotEvents\Traits\PivotEventTrait trait in your base model or only in particular models.
    // ...
    use GeneaLabs\LaravelPivotEvents\Traits\PivotEventTrait;
    use Illuminate\Database\Eloquent\Model;
    
    abstract class BaseModel extends Model
    {
        use PivotEventTrait;
        // ...
    }

New Eloquent Events

You can check all eloquent events here: https://laravel.com/docs/5.8/eloquent#events)

New events are :

  • pivotSyncing, pivotSynced
  • pivotAttaching, pivotAttached
  • pivotDetaching, pivotDetached
  • pivotUpdating, pivotUpdated

The easiest way to catch events is using methods in your model's boot() method:

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

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

    static::pivotSynced(function ($model, $relationName, $changes) {
        //
    });

    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 catch them using dedicated Event Listeners:

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

Supported Relationships

BelongsToMany and MorphToMany

Which events are dispatched and when they are dispatched

Four BelongsToMany methods dispatches events from this package :

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.

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.

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

sync() Dispatches one pivotSyncing and one pivotSynced event. Whether a row was attached/detached/updated during sync only one event is dispatched for all rows but in that case, you can see all the attached/detached/updated rows in the $changes variables. E.g. How does sync work: The sync first detaches all associations and then attaches or updates new entries one by one.

Usage

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

class User extends Model
{
    use PivotEventTrait;
    // ...

    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }

    static::pivotSynced(function ($model, $relationName, $changes) {
        echo 'pivotSynced';
        echo get_class($model);
        echo $relationName;
        print_r($changes);
    });

    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.

Attaching With Primary Key

Running this code

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

You will see this output

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

Attaching with array

Running this code

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

You will see this output

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

Attaching with model

Running this code

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

You will see this output

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

Attaching with collection

Running this code

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

You will see this output

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

Attaching with array (id => attributes)

Running this code

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

You will see this output

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

Syncing

Running this code

$user = User::first();
$user->roles()->attach([
     1 => ['pivot_attribut' => 1],
     2 => ['pivot_attribut' => 0]
 ]);
 $user->roles()->sync([
     1 => ['pivot_attribut' => 0]
     3 => ['pivot_attribut' => 1]
 ]);

You will see this output

pivotSynced
App\Models\User
roles
[
   "attached" => [
     0 => 3
   ]
   "detached" => [
     1 => 2
   ]
   "updated" => [
     0 => 1
   ]
 ]

Detaching

Running this code

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

You will see this output

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

Updating

Running this code

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

You will see this output

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

mikebronner/laravel-pivot-events 适用场景与选型建议

mikebronner/laravel-pivot-events 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 757.2k 次下载、GitHub Stars 达 141, 最近一次更新时间为 2025 年 02 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel pivot events」 「laravel BelongsToMany events」 「eloquent extra events」 「laravel sync events」 「eloquent events」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 mikebronner/laravel-pivot-events 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mikebronner/laravel-pivot-events 我们能提供哪些服务?
定制开发 / 二次开发

基于 mikebronner/laravel-pivot-events 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 757.2k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 141
  • 点击次数: 26
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 141
  • Watchers: 1
  • Forks: 12
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-26