Laravel withTrashed仅当父母被垃圾桶

时间:2018-08-27 05:30:56

标签: php laravel eloquent

我有评论部分,但我只想检索已删除的评论(仅当父级实际上已删除时)

Invite::forWedding($wedding)->withTrashed()->with([
        "comments" => function ($query) {
            // I want to check that "Invite" is trashed , else return unfiltered;
            return $query->withTrashed();
        }
        , "comments.user"
    ]
)->findOrFail($id);

1 个答案:

答案 0 :(得分:0)

我认为您可以直接查询父表中的deleted_at列。

也许是这样的:

Invite::forWedding($wedding)->withTrashed()->with([
        "comments" => function ($query) {
            $query->whereNotNull('comments.deleted_at');
            $query->whereNotNull('invites.deleted_at');
        }
        , "comments.user"
    ]
)->findOrFail($id);