如何将多个查询包装到 laravel 中的单个 where 子句中

How to wrap mutliple queries into a single where clause in laravel

我想从 table pasien_polis 中获取记录,其中 polyclinic_id 匹配 polyclinics table 的 id

我可以通过这样做来实现:

    $polyclinic = Polyclinic::findOrFail($id);
    $pasienpoli = Polyclinic::find($id)->PasienPoli;

接下来我要过滤的是created_at条记录,我只想获取今天创建的记录。 此查询运行良好:

    $pasienpoli = DB::table('pasien_polis')->whereDate('created_at', '=', \Carbon\Carbon::today()->toDateString())->get();

当我想将这两个过滤器组合成一个 where 子句时,问题出现了。

我尝试了以下方法,但它 returns NULL:

$polyclinic = Polyclinic::findOrFail($id);
$match = Polyclinic::find($id)->PasienPoli;

$pasienpoli = DB::table('pasien_polis')->where([
                     ['polyclinic_id', '=', '$match'],
                     ['created_at', '=', \Carbon\Carbon::today()->toDateString()]
                                               ])->get();

有什么帮助吗?

更新:

我想你可以试试这个:

    $pasienpoli = DB::table('pasien_polis')
                        ->join('polyclinics','polyclinics.id','=','pasien_polis.polyclinic_id')
                        ->where([
                         ['pasien_polis.polyclinic_id', '=', $id],
                         ['pasien_polis.created_at', '=', \Carbon\Carbon::today()->toDateString()]])
                       ->get();

希望对您有所帮助!

通过修改 @AddWeb Solution Pvt 的答案解决(非常感谢)。
这是我最后的工作查询:

$pasienpoli = DB::table('pasien_polis')
  ->join('polyclinics','polyclinics.id','=','pasien_polis.polyclinic_id')
  ->where([
  ['pasien_polis.polyclinic_id', '=', $id],
  [DB::raw('DATE(pasien_polis.created_at)'), '=', \Carbon\Carbon::today()->toDateString()]
  ])->get();