Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WhereNotExists Laravel Eloquent

Little bit of trouble with the eloquent framework for 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
like image 782
Sytham Avatar asked Jul 25 '16 16:07

Sytham


2 Answers

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();
like image 120
Kamil Kiełczewski Avatar answered Nov 20 '22 19:11

Kamil Kiełczewski


Try doesntHave() method. Assuming 'dismissedRequests' as relation name in RepairJob model.

$jobs = RepairJob::with('repairJobPhoto', 'city', 'vehicle')
    ->where('active', 'Y')->doesntHave('dismissedRequests')->get();
like image 44
Phargelm Avatar answered Nov 20 '22 20:11

Phargelm