WhereNotExists Laravel Eloquent

Syt*_*ham 8 php laravel eloquent

laravel的雄辩框架有点麻烦.

我需要像这样复制一个查询:

SELECT *
FROM RepairJob
WHERE NOT EXISTS (SELECT repair_job_id
    FROM DismissedRequest
    WHERE RepairJob.id = DismissedRequest.repair_job_id);
Run Code Online (Sandbox Code Playgroud)

现在我有

 $repairJobs = RepairJob::with('repairJobPhoto', 'city', 'vehicle')->where('active', '=', 'Y')->whereNotExists('id', [DismissedRequest::all('repair_job_id')])->get();
Run Code Online (Sandbox Code Playgroud)

有人有想法吗?我需要获取所有在被解雇的请求表中没有记录的repairjobs

使用上面的查询时出现此错误

Argument 1 passed to Illuminate\Database\Query\Builder::whereNotExists() must be an instance of Closure, string given
Run Code Online (Sandbox Code Playgroud)

Pha*_*elm 7

尝试didtHave()方法。在RepairJob模型中,假设“ dismissedRequests”为关系名称。

$jobs = RepairJob::with('repairJobPhoto', 'city', 'vehicle')
    ->where('active', 'Y')->doesntHave('dismissedRequests')->get();
Run Code Online (Sandbox Code Playgroud)


Kam*_*ski 6

尝试这个:

$repairJobs = RepairJob::with('repairJobPhoto', 'city', 'vehicle')
              ->where('active', '=', 'Y')
              ->whereNotExists(function($query)
                {
                    $query->select(DB::raw(1))
                          ->from('DismissedRequest')
                          ->whereRaw('RepairJob.id = DismissedRequest.id');
                })->get();
Run Code Online (Sandbox Code Playgroud)

  • 我建议使用 `whereColumn` 而不是 `whereRaw` (3认同)