Laravel调用软删除的关联模型

admin 阅读:39 2024-03-02

在关联关系中定义包含软删除内容:

public function roles() {
    return $this->hasMany(Role::class)->withTrashed();
}

在模型列表中包含软删除内容:

$user->roles()->withTrashed()->get();

在预加载中包含软删除内容:

$user = User
   ::with(['roles' => fn($q) => $q->withTrashed()])
   ->findOrFail($id);
声明

1、部分文章来源于网络,仅作为参考。
2、如果网站中图片和文字侵犯了您的版权,请联系1943759704@qq.com处理!

搜索