如何在laravel中传播属于该关系的更新

时间:2015-04-11 18:04:18

标签: laravel-4 relationship

我有两个型号Shelf,Book and Quote

class Shelf extends Eloquent {
    public function quotes()
    {
        return $this->belongsToMany('Book');
    }
}

class Book extends Eloquent {
    protected $touches = array('shelfs');
    public function quotes()
    {
        return $this->belongsToMany('Quote');
    }

    public function shelfs()
    {
        return $this->belongsToMany('Shelf');
    }
}

class Quote extends Eloquent {
    protected $touches = array('books');

    public function books()
    {
        return $this->belongsToMany('Book')->withTimestamps();
    }
}

当我更新Qoute模型时,Qout模型表中的updated_at被更新,但是架子模型没有更新。

我做错了吗? 提前谢谢

1 个答案:

答案 0 :(得分:1)

在早期版本中,您只能更新在其自己的模型中$touch数组中声明的一个级别关系,而不是更新来自父模型。您需要更新到v4.2.9以上才能获得此功能,因为直到v4.2.8才会包含此功能。为了更安全,尝试从v4.2.10开始使用,其中包括检查关系是否为空。

修改

需要升级到v5以获得@Dusan Plavak在评论中提到的内容。

希望它对你有用。