【问题标题】:How to propagate update in belongstomany relationship in laravel如何在laravel中的belongstomany关系中传播更新
【发布时间】:2015-04-11 18:04:17
【问题描述】:

我有两个模型 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 模型表中的 updated_at 会更新,但 Shelf 模型不会。

我做错了吗? 提前谢谢

【问题讨论】:

    标签: laravel-4 relationship


    【解决方案1】:

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

    编辑

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

    希望对你有用。

    【讨论】:

    • 嗯,谢谢,但实际上在 v4.2.10 中它并没有修复。很遗憾,即使在 v4.2.17 中它也没有修复。但它已在v5 中修复,并且他们不会进行反向移植...如果您要编辑您的答案,我无论如何都会接受它,因为它指出了我发现这一点的正确方法
    猜你喜欢
    • 2022-01-16
    • 2018-05-19
    • 2023-03-12
    • 1970-01-01
    • 2015-02-22
    • 1970-01-01
    • 2020-05-30
    • 2016-07-13
    • 1970-01-01
    相关资源
    最近更新 更多