eloquent-attach-detach-sync-fires-any-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)?

  • 1
    As far as I know there is no event called. However you could use the event handlers to fire one – cleanunicornMar 8 '15 at 18:50
  •  
    Thanks! @hydrarulz yes but I will have to take care to fire it manually every time I use attach on that specific model, not optimal – Mihai Crăiță Mar 8 '15 at 20:27

2 Answers

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.

最新文章

  1. 2.EasyUI学习总结(二)——easyloader分析与使用(转载)
  2. bzoj 3504: [Cqoi2014]危桥
  3. C语言(4)
  4. OpenHCI - 4.2 Endpoint Descriptor
  5. github上所有项目的受欢迎程度排名,包括超大型项目
  6. Android应用开发学习笔记之Intent
  7. CSS3 @font-face使用实例
  8. windows身份验证,那么sqlserver的连接字符串的
  9. java split小结(转)
  10. maven插件报错之解决
  11. Mysql 分区详解
  12. [js高手之路]Node.js+jade+mongoose实战todolist(分页,ajax编辑,删除)
  13. linkin大话面向对象--闭包和回调
  14. Flask简介&amp;入门
  15. JAVA 连等赋值问题
  16. Puppet部署Nginx返代示例
  17. vue项目中全局配置变量
  18. 我所理解的Delphi中的数组类型
  19. Yarn资源调度过程详细
  20. 推荐一个在线json数据格式化网站

热门文章

  1. APP支付-》支付宝RSA2-&gt;支付与验签
  2. dubbo 实战
  3. 【selenium+python】关于使用selenium时的几个问题1
  4. centos 6 KVM 网卡桥接配置
  5. CSS3实现10种Loading效果(转)
  6. unity实现3D物体上的事件监听处理
  7. stm32DMA
  8. ActiveMQ之java Api
  9. 速卖通API开发步骤
  10. HUABASE :基于列存储的关系型数据库系统