WhereNotExists Laravel Eloquent
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38572706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WhereNotExists Laravel Eloquent
提问by Sytham
Little bit of trouble with the eloquent framework for laravel.
laravel 的雄辩框架有点麻烦。
I need to replicate a query like this :
我需要复制这样的查询:
SELECT *
FROM RepairJob
WHERE NOT EXISTS (SELECT repair_job_id
FROM DismissedRequest
WHERE RepairJob.id = DismissedRequest.repair_job_id);
Right now I have
现在我有
$repairJobs = RepairJob::with('repairJobPhoto', 'city', 'vehicle')->where('active', '=', 'Y')->whereNotExists('id', [DismissedRequest::all('repair_job_id')])->get();
Anyone an idea? I need to get all the repairjobs where there is no record for in the dismissed requests table
任何人的想法?我需要获取在已驳回请求表中没有记录的所有维修作业
I get this error when using the query above
使用上面的查询时出现此错误
Argument 1 passed to Illuminate\Database\Query\Builder::whereNotExists() must be an instance of Closure, string given
采纳答案by Phargelm
Try doesntHave() method. Assuming 'dismissedRequests' as relation name in RepairJob model.
尝试 dontHave() 方法。假设“dismissedRequests”作为 RepairJob 模型中的关系名称。
$jobs = RepairJob::with('repairJobPhoto', 'city', 'vehicle')
->where('active', 'Y')->doesntHave('dismissedRequests')->get();
回答by Kamil Kie?czewski
Try this:
尝试这个:
$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();