Laravel 5 >触摸数据透视表(多对多)



为什么数据透视表中updated_at字段没有更新,并给出以下设置?

class Conversation extends Model {
    public function users()
    {
       return $this->belongsToMany('AppUser')->withTimestamps();
    }
}

控制器

$conversation = Conversation::find($id);
$conversation->users()->touch();

最后一行应该 - 据我了解整个事情 - 也更新数据透视表updated_at字段,不是吗?

但事实并非如此。现在我必须通过数据库查询手动完成。

我认为最好的方法是:

$conversation->users()->updateExistingPivot(
    $conversation->users()->get(),
    ['updated_at' => date('Y-m-d H:i:s')]
);

它对我有用(Laravel 5.7)。

最新更新