laravel 雄辩的附加/分离/同步触发任何事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28925292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:05:16  来源:igfitidea点击:

Eloquent attach/detach/sync fires any event?

phplaraveleloquent

提问by Mihai Cr?i??

I have a laravel project, and I need to make some calculations immediately after I save a model and attach some data to it.

我有一个 Laravel 项目,我需要在保存模型并附加一些数据后立即进行一些计算。

Is there any event that is triggered in laravel after calling attach (or detach/sync)?

调用 attach(或 detach/sync)后,laravel 中是否有触发事件?

采纳答案by fico7489

Steps to solve your problem:

解决您的问题的步骤:

  1. Create custom BelongsToMany relation
  2. In BelongsToMany custom relation override attach, detach, sync and updateExistingPivot methods
  3. In overriden method dispatch desired events.
  4. Override belongsToMany() method in Model and return your custom relation not default relation
  1. 创建自定义 BelongsToMany 关系
  2. 在 BelongsToMany 自定义关系中覆盖 attach、detach、sync 和 updateExistingPivot 方法
  3. 在覆盖方法中调度所需的事件。
  4. 覆盖模型中的belongsToMany() 方法并返回您的自定义关系而不是默认关系

and that's it. I created package that already doing that: https://github.com/fico7489/laravel-pivot

就是这样。我创建了已经这样做的包:https: //github.com/fico7489/laravel-pivot

回答by Jarek Tkaczyk

No, there are no relation events in Eloquent. But you can easily do it yourself (Given for example Ticket belongsToMany Componentrelation):

不,在 Eloquent 中没有关系事件。但是你可以很容易地自己做(例如Ticket belongsToMany Component关系):

// Ticket model
use App\Events\Relations\Attached;
use App\Events\Relations\Detached;
use App\Events\Relations\Syncing;
// ...

public function syncComponents($ids, $detaching = true)
{
    static::$dispatcher->fire(new Syncing($this, $ids, $detaching));

    $result = $this->components()->sync($ids, $detaching);

    if ($detached = $result['detached'])
    {
        static::$dispatcher->fire(new Detached($this, $detached));
    }

    if ($attached = $result['attached'])
    {
        static::$dispatcher->fire(new Attached($this, $attached));
    }
}

event object as simple as this:

事件对象就这么简单:

<?php namespace App\Events\Relations;

use Illuminate\Database\Eloquent\Model;

class Attached {

    protected $parent;
    protected $related;

    public function __construct(Model $parent, array $related)
    {
        $this->parent    = $parent;
        $this->related   = $related;
    }

    public function getParent()
    {
        return $this->parent;
    }

    public function getRelated()
    {
        return $this->related;
    }
}

then a basic listener as a sensible example:

那么一个基本的听众作为一个明智的例子:

    // eg. AppServiceProvider::boot()
    $this->app['events']->listen('App\Events\Relations\Detached', function ($event) {
        echo PHP_EOL.'detached: '.join(',',$event->getRelated());
    });
    $this->app['events']->listen('App\Events\Relations\Attached', function ($event) {
        echo PHP_EOL.'attached: '.join(',',$event->getRelated());
    });

and usage:

和用法:

$ php artisan tinker

>>> $t = Ticket::find(1);
=> <App\Models\Ticket>

>>> $t->syncComponents([1,3]);

detached: 4
attached: 1,3
=> null

Of course you could do it without creating Event objects, but this way is more convenient, flexible and simply better.

当然你也可以不创建 Event 对象,但这种方式更方便、更灵活、更简单。

回答by Twostein

Laravel 5.8 now fires events on ->attach()

Laravel 5.8 现在在 ->attach() 上触发事件

Check out: https://laravel.com/docs/5.8/releases

查看:https: //laravel.com/docs/5.8/releases

And search for: Intermediate Table / Pivot Model Events

并搜索:中间表/枢轴模型事件

https://laracasts.com/discuss/channels/eloquent/eloquent-attach-which-event-is-fired?page=1

https://laracasts.com/discuss/channels/eloquent/eloquent-attach-which-event-is-fired?page=1