laravel - 多对多关系的中间表上的软删除

标签 laravel laravel-4

如何在连接两种不同类型实体的中间表上设置软删除?我添加了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/

相关文章:

javascript - Axios 响应数据未加载到 Laravel 中的 Vue 组件上

cookies - 无法在 Laravel 4 中设置 cookie

php - 几乎所有查询都变慢了

php - Eloquent 模型上的保留字

php - 使用 Eloquent 将 Laravel 4 与 3 个表联系起来

php - Laravel 5.1 中未定义的默认命名空间

mysql - 将有关 Viewers Laravel 的数据插入 Mysql

php - Laravel:更新时唯一的验证

Laravel Homestead : Nginx failing to start on Vagrant. 需要root密码才能访问Nginx日志

php - Laravel 4.2 artisan CLI 在 composer 更新后不再工作