雄辩的附加/分离/同步触发任何事件? [英] Eloquent attach/detach/sync fires any event?

查看:85
本文介绍了雄辩的附加/分离/同步触发任何事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



在调用attach(或分离/同步)后,是否有任何在laravel中触发的事件?

解决方案

不,在雄辩中没有关系事件。但是你可以很容易地做到这一点(例如 Ticket belongsToMany Component relation):

  //票证模型
使用App \Events\Relations\Attached;
使用App\Events\Relations\Detached;
使用App \Events\Relations\Syncing;
// ...

public function syncComponents($ ids,$ detaching = true)
{
static :: $ dispatcher-> fire(new Syncing ($ this,$ id,$ 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));
}
}

事件对象简单如下:

 <?php命名空间App \Events\Relations; 

使用Illuminate\Database\Eloquent\Model;

class附加{

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

然后一个基本的监听器是一个明智的例子: p>

  // 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());
});

和用法:

  $ php artisan tinker 

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

>>> $叔> syncComponents([1,3]);

已分离:4
附:1,3
=> null

当然,您可以在不创建Event对象的情况下执行此操作,但这样更方便,灵活,更好。


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

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

解决方案

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

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

这篇关于雄辩的附加/分离/同步触发任何事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆