如何在Laravel

时间:2019-08-29 06:32:38

标签: laravel laravel-5 laravel-query-builder

我是Laravel新手。     在下面,我提到了我的代码如何在laravel查询生成器中实现此功能。

使用的表格是ab,cd,ef,gh,ij

 SELECT ab.* FROM ab
    WHERE ab.id IN (
      SELECT ab_id FROM cd
      WHERE ef_id = 1 AND status = 1
      AND deleted_at IS NULL
      AND ab_id IN (
        SELECT ab_id FROM gh
         WHERE ij_id IN (
            SELECT id FROM ij
            WHERE ef_id = 1 AND deleted_at IS NULL
        ) AND deleted_at IS NULL
      ) AND ab_id IN (
          SELECT id FROM ab
           WHERE deleted_at IS NULL AND usertest != 1
          )
      )AND ab.deleted_at IS NULL
GROUP BY ab.id
ORDER BY ab.created_at DESC;

1 个答案:

答案 0 :(得分:0)

您可以将闭包传递给where条件并查询另一个表。

来自Laravel docs

DB::table('users')
            ->whereExists(function ($query) {
                $query->select(DB::raw(1))
                      ->from('orders')
                      ->whereRaw('orders.user_id = users.id');
            })
            ->get();