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.

Eloquent Attach/Detach/Sync Fires Any Event的更多相关文章

  1. laravel 多对多关联 attach detach sync

    用户表和角色表,多对多关联,一个用户有多个角色,一个角色属于多个用户 添加多对多关联 attach: 给1号用户添加1号角色,并把关联表的column字段赋值为$value,后边的数组需要的时候再添加 ...

  2. 揭秘 Kubernetes attach/detach controller 逻辑漏洞致使 pod 启动失败

    前言 本文主要通过深入学习k8s attach/detach controller源码,了解现网案例发现的attach/detach controller bug发生的原委,并给出解决方案. 看完本文 ...

  3. Entity Framework 数据部分更新之Attach &&Detach

    我们经常会遇到这样的问题:Update一个entity的部分数据时,通常需要new一个新的对象,然后事这新的对象Attach到Context中,代码如下所示: /// <summary> ...

  4. [转] Attach、Detach和DeleteObject

    原文:Attach.Detach和DeleteObject,想飞的梦想 1.CWnd Attatch和Detach的关系 首先,要明白Windows对象和MFC对象的区别. MFC对象实际上并没有把整 ...

  5. MFC Attach()函数和Detach()函数

    一.Windows对象和MFC对象的区别?MFC对象实际上并没有把整个Windows对象都包装在其中.对于窗口:MFC对象它只是有一个窗口句柄而已,这个窗口句柄如果指向一个实际存在的窗口对象(窗口对象 ...

  6. Attach()和Detach()函数

    一.Windows对象和MFC对象的区别? MFC对象实际上并没有把整个Windows对象都包装在其中.对于窗口:MFC对象它只是有一个窗口句柄而已,这个窗口句柄如果指向一个实际存在的窗口对象(窗口对 ...

  7. Attach、Detach和DeleteObject

    原文:Attach.Detach和DeleteObject,想飞的梦想 1.CWnd Attatch和Detach的关系 首先,要明白Windows对象和MFC对象的区别. MFC对象实际上并没有把整 ...

  8. Yii2的深入学习--yii\base\Event 类

    根据之前一篇文章,我们知道 Yii2 的事件分两类,一是类级别的事件,二是实例级别的事件.类级别的事件是基于 yii\base\Event 实现,实例级别的事件是基于 yii\base\Compone ...

  9. 转载JQuery 中empty, remove 和 detach的区别

    转载 http://www.cnblogs.com/lisongy/p/4109420.html .empty()  描述: 从DOM中移除集合中匹配元素的所有子节点. 这个方法不接受任何参数. 这个 ...

随机推荐

  1. 大型运输行业实战_day14_1_webserivce简单入门

    1.简单使用 1.1.服务端 1.编写接口 package com.day02.sation.ws; /** * Created by Administrator on 1/12. */ public ...

  2. docker 安装与使用

    1.yum安装docker yum -y install docker 2.启动docker service docker start 3.检验运行状态 systemctl status docker ...

  3. CentOS Netstat命令

    语法 netstat(选项) 选项 -a或--all:显示所有连线中的Socket: -A<网络类型>或--<网络类型>:列出该网络类型连线中的相关地址: -c或--conti ...

  4. 51nod 1459 迷宫游戏 dijkstra模板

    链接:迷宫游戏 问题 - 51Nod  http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1459 1459 迷宫游戏  基准 ...

  5. 204. Count Primes (Integer)

    Count the number of prime numbers less than a non-negative number, n. 思路:寻找质数的方法 class Solution { pu ...

  6. 使用vue-cli快速搭建大型单页应用

    前言: 经过一段时间angular的洗礼之后 ,还是决定回归Vue.现就vue安装.工程搭建.常用依赖安装直至开发挣个流程做一整理,希望对初学者有所帮助. 前提条件: 对 Node.js 和相关构建工 ...

  7. TZOJ 4712 Double Shortest Paths(最小费用最大流)

    描述 Alice and Bob are walking in an ancient maze with a lot of caves and one-way passages connecting ...

  8. [HDOJ]Coin Change(DP)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2069 题意 有面值1,5,10,25,50的硬币数枚,对于输入的面值n,输出可凑成面值n(且限制总硬笔 ...

  9. ubuntu下安装、破解securtCRT工具

    官网:https://www.vandyke.com/ 下载好的安装包以及破解工具的网盘地址:链接: https://pan.baidu.com/s/1UEKEy-aob7WdfPLR4PNJmg 提 ...

  10. mysql-5.7.21安装和配置方法

      到MySQL官网下载MySQL 5.7.21的版本,下载地址:https://dev.mysql.com/downloads/mysql/ .根据自己的系统下载相应的32位或64位的版本. 将下载 ...