如何在 laravel 的 belongstomany 关系中传播更新

How to propagate update in belongstomany relationship in laravel

我有两个模型 Shelf、Book 和 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 模型 table 中的 updated_at 被更新,但 Shelf 模型没有。

我是不是做错了什么? 提前致谢

在早期版本中,您只能更新在其自身模型中的 $touch 数组中声明的一个级别关系,还不能从父模型中更新。您需要更新到上面的 v4.2.9 to get this feature because it is not included till v4.2.8. To be more safe, try to use starting from v4.2.10,其中包括检查关系是否为空。

编辑

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

希望对你有用。