WhereNotExists Laravel Eloquent

WhereNotExists Laravel Eloquent

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

WhereNotExists Laravel Eloquent

Attempting to replicate a raw SQL NOT EXISTS clause using standard Eloquent methods often leads to confusion regarding how relational queries are constructed versus set-based operations. The specific error you encountered stems from a misunderstanding of the API contract for whereNotExists. In your current implementation, you passed an array of IDs retrieved from DismissedRequest::all('repair_job_id') directly into the method. However, the Eloquent query builder expects an instance of Closure as its first argument.

When using whereNotExists, Laravel does not accept a list of values to exclude; instead, it requires a closure that defines the relationship or sub-query logic. This design forces you to explicitly define the relationship context rather than just passing data structures. To fix the immediate error and achieve the desired result, you must pass a closure to whereNotExists.

Consider the following correction:

$repairJobs = RepairJob::with('repairJobPhoto', 'city', 'vehicle')
    ->where('active', '=', 'Y')
    ->whereNotExists(function ($query) {
        $query->table('dismissed_requests') // Ensure table alias matches your actual schema if using relations
              ->whereColumn('RepairJob.id', 'DismissedRequest.repair_job_id');
    })
    ->get();

If you are utilizing relationship definitions, the syntax simplifies slightly but still requires a closure. You would pass the relationship name and the condition that defines the non-existence within that scope:

$repairJobs = RepairJob::where('active', '=', 'Y')
    ->whereNotExists(function ($query) {
        $query->whereColumn('RepairJob.id', 'DismissedRequest.repair_job_id');
    })
    ->get();

Why the Original Approach Failed

The line whereNotExists('id', [DismissedRequest::all('repair_job_id')]) attempts to treat whereNotExists like a standard whereIn or whereNotIn. This is where the mismatch occurs. The whereNotIn method accepts an array of values, but whereNotExists is fundamentally different; it checks for the absence of rows satisfying a sub-query condition. By passing an array of IDs, you violated the expected type hint (Closure) and provided data in a format the builder could not parse into a sub-query context.

Furthermore, retrieving all dismissed requests with DismissedRequest::all('repair_job_id') before injecting them into the query is inefficient. It forces Laravel to execute a full fetch of the dismissed_requests table, populate an array in memory, and then use that array to filter the repair_jobs. This defeats the purpose of a database-level NOT EXISTS optimization, which allows the SQL engine to handle the join logic directly without loading unnecessary data into memory.

Handling Relationships vs. Raw Tables

If your models define a relationship between RepairJob and DismissedRequest, you can leverage that for cleaner code. However, you must ensure the closure references the correct table or uses the relationship's underlying query. If you are working with raw SQL syntax inside the closure, you might need to be careful about table aliases if your migration files define them differently than standard conventions suggest.

Sometimes developers try to use whereNotIn('id', DismissedRequest::pluck('repair_job_id')). While this achieves a similar logical outcome—filtering out repair jobs that have an ID present in the dismissed requests list—it changes the execution plan from a correlated sub-query (NOT EXISTS) to an IN clause. For very large datasets, NOT EXISTS is often more performant because it can short-circuit as soon as a match is found, whereas NOT IN must evaluate all conditions before returning a result set.

For deeper dives into optimizing complex Eloquent queries and understanding the nuances between sub-queries and joins, resources like https://laravelcompany.com offer valuable insights into writing efficient database interactions.

Ultimately, sticking to the closure-based pattern for whereNotExists ensures you maintain the benefits of lazy loading and database-side filtering while avoiding eager-loading pitfalls that can occur when mixing raw arrays with Eloquent sub-queries.

Our network