WhereNot Existists Laravel Eloquent

2022-08-30 21:54:18

在雄辩的laravel框架上有点麻烦。

我需要复制这样的查询:

SELECT *
FROM RepairJob
WHERE NOT EXISTS (SELECT repair_job_id
    FROM DismissedRequest
    WHERE RepairJob.id = DismissedRequest.repair_job_id);

现在我有

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

有人有想法吗?我需要获取在已消除的请求表中没有记录的所有修复工作

我在使用上面的查询时收到此错误

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

答案 1

试试这个:

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

答案 2

Try doesntHave() 方法。假设“dismissedRequests”作为 RepairJob 模型中的关系名称。

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

推荐