Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Laravel中调用attach或detach时,如何让“pivot table”模型触发保存/保存的模型事件?_Php_Laravel_Laravel 4_Eloquent - Fatal编程技术网

Php 在Laravel中调用attach或detach时,如何让“pivot table”模型触发保存/保存的模型事件?

Php 在Laravel中调用attach或detach时,如何让“pivot table”模型触发保存/保存的模型事件?,php,laravel,laravel-4,eloquent,Php,Laravel,Laravel 4,Eloquent,在Laravel 4中,当调用“附加”或“分离”时,如何使透视表模型触发保存/保存的模型事件 似乎下面的数据透视表“TeamUser”实际上并不需要attach/detach方法来工作,因此我猜想表示数据透视表的模型永远不会被调用。所以这些事件永远不会被触发 换一种说法: 当我调用User::with('Team')->find(1)->teams()->attach(1)如何获取TeamUser来触发它自己的事件。请注意,上面的附加工作非常好,所有记录都在数据库中更新 用户 class Use

在Laravel 4中,当调用“附加”或“分离”时,如何使
透视表
模型触发保存/保存的模型事件

似乎下面的数据透视表“TeamUser”实际上并不需要attach/detach方法来工作,因此我猜想表示数据透视表的模型永远不会被调用。所以这些事件永远不会被触发

换一种说法: 当我调用
User::with('Team')->find(1)->teams()->attach(1)如何获取
TeamUser
来触发它自己的事件。请注意,上面的附加工作非常好,所有记录都在数据库中更新

用户

class User extends Eloquent 
{
    // Relationship
    public function teams() 
    {
        return $this->belongsToMany('Team');
    }    
}
团队

class Team extends Eloquent 
{
    // Relationship
    public function users() 
    {
        return $this->belongsToMany('User');
    }    
}
团队用户-团队用户
表/透视表的模型

class TeamUser extends Eloquent 
{
    public function save(array $options = array())
    {
        // do cool stuff when relationship is saved via
        // attach/detach
    }
}

您可以将
$touchs
添加到团队模型中

class团队扩展了雄辩的
{
受保护的$touchs=数组('TeamUser');
//关系
公共功能用户()
{
返回$this->belongstomy('User');
}    
}
这将在字段中更新TeamUser
updated\u。我认为这也会影响TeamUser模型中的
save()
方法

如果这不起作用,您可以在Team
save()
方法中触发自己的事件,并在某处侦听此事件

Event::fire('team.updated',数组($team));

在新版本的Laraval 5.8中,透视表触发启动事件。检查发行说明:

当团队接触TeamUser时,这并不意味着调用attach()或detach()。激发Team::save()时,也不意味着调用attach()或detach()。为什么这个答案被接受?我也在寻找解决方案,但这个imo不是。@RonaldHulshof你可以收听pivot table的更新,这将确保。更新将非常棒!非常感谢。我一直在找这个。