laravel雄辩-> whereHas-编写自己的存在(子查询)

时间:2018-08-29 05:50:32

标签: php mysql laravel eloquent exists

Laravel口才->whereHas()使用exists()子查询https://dev.mysql.com/doc/refman/8.0/en/exists-and-not-exists-subqueries.html来返回结果。

我想编写自己的子查询,但是我不知道如何告诉Eloquent->它在哪里。

如果我这样做:

$query->where( DB::raw(' exists( subquery ) ')

Laravel将子查询写为:

where exists( subquery ) is null

因此,我只是想知道可以使用什么$query->method()向'where'语句中添加一个exist()子查询。子查询将与laravel生成的子查询相同,但会写出来:

... and exists ( select * from `tbl` inner join `assets` on `custom_assets`.`id` = `tbl`.`asset_id` where `assets`.`deleted_at` is null and `users`.`id` = `assets`.`client_id` and `field_id` = ? and (`value` = ? and `assets`.`deleted_at` is null )

2 个答案:

答案 0 :(得分:0)

Read WhereHas Description Here

您可以在此处找到此代码示例。您还可以在whereHas中为自定义查询添加一个闭包。

// Retrieve all posts with at least one comment containing words like foo%
$posts = App\Post::whereHas('comments', function ($query) {
    $query->where('content', 'like', 'foo%');
})->get();

答案 1 :(得分:0)

使用whereRaw()

$query->whereRaw('exists( subquery )')
相关问题