【问题标题】:laravel eloquent ->whereHas - write your own exists( subquery )laravel eloquent ->whereHas - 编写你自己的存在(子查询)
【发布时间】:2019-02-03 19:42:03
【问题描述】:

Laravel Eloquent ->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() 可以用来将exists() 子查询添加到“where”语句中。子查询与 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 )

【问题讨论】:

  • 你能举一个你想写的子查询的例子吗?

标签: php mysql laravel eloquent exists


【解决方案1】:

使用whereRaw():

$query->whereRaw('exists( subquery )')

【讨论】:

    【解决方案2】:

    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();
    

    【讨论】:

      猜你喜欢
      • 2021-05-25
      • 1970-01-01
      • 2021-05-28
      • 1970-01-01
      • 2018-02-27
      • 2020-04-22
      • 2021-09-05
      • 2022-08-19
      • 2021-08-23
      相关资源
      最近更新 更多