如何在连接两种不同类型实体的中间表上设置软删除?我添加了deleted_at 列,但文档说我需要将其放入模型中:
protected $softDelete = true;
当然,我没有中间表的模型。
任何的想法?
最佳答案
您可以对 Eager Load 施加约束:
public function groups()
{
return $this
->belongsToMany('Group')
->whereNull('group_user.deleted_at') // Table `group_user` has column `deleted_at`
->withTimestamps(); // Table `group_user` has columns: `created_at`, `updated_at`
}
而不是 HARD 删除关系使用:
User::find(1)->groups()->detach();
你应该使用这样的东西来软删除:
DB::table('group_user')
->where('user_id', $user_id)
->where('group_id', $group_id)
->update(array('deleted_at' => DB::raw('NOW()')));
关于laravel - 多对多关系的中间表上的软删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17350072/