Laravel Eloquent OR WHERE IS NOT NULL

Laravel Eloquent OR WHERE IS NOT NULL

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

Stefan Bogdanescu

Founder & Senior Architect

Laravel Eloquent OR WHERE IS NOT NULL

When working with soft-deleted models in Laravel, the default behavior of withTrashed() or explicitly querying for non-deleted records usually satisfies most use cases. However, scenarios like those encountered with the Frozennode Administrator package can demand a specific hybrid view: displaying only active rows when filtering normally, but showing all rows (active and deleted) when a specific toggle is engaged.

The core issue arises when attempting to construct a query that effectively returns "everything" by combining is null and is not null conditions using orWhere. The user wants to execute SQL equivalent to:

select * from `scripts` where `scripts`.`deleted_at` is null or `scripts`.`deleted_at` is not null group by `scripts`.`id`

Logically, this condition covers the entire domain of the deleted_at column. In pure SQL, this works because is null and is not null are mutually exclusive; every row will satisfy at least one of them. However, Laravel's query builder is strict about how boolean logic is translated into SQL tokens.

The problematic implementation involves passing a raw SQL block to orWhere. When you use $query->orWhere(DB::raw('...')), Laravel expects the segment inside orWhere to be a complete condition that stands alone in the final WHERE clause. The resulting malformed query:

select * from `scripts` where `scripts`.`deleted_at` is null or `scripts`.`deleted_at` is not null **is null** group by `scripts`.`id`

reveals exactly what went wrong. The builder appended an extra is null token because the raw string provided was treated as a fragment rather than a complete clause, or the binding logic failed to recognize the or operator within the raw block correctly in conjunction with the preceding filter.

To resolve this, you must ensure that the condition passed to orWhere is syntactically complete and does not rely on implicit operators that the builder might misinterpret. Instead of trying to hack the deleted_at column directly with a fragment, consider using a raw query block that encapsulates the entire logic as a single unit, or explicitly define the scope within the model.

A more robust approach in Laravel is to leverage a custom scope that handles the "show all" state. You can define a method in your Script model:

public static function withDeleted()
{
    return new QueryBuilder(
        config('database.default'), 
        config('database.connections.' . config('database.default')).getSchemaName(), 
        'scripts'
    );
}

However, if you are strictly working within the Administrator package's query_filter array where dynamic SQL injection is expected, the syntax needs to be precise. The error often stems from how Laravel binds values to raw expressions. If you are trying to bypass the soft delete filter entirely, simply omitting the condition or using a tautology might be cleaner than fighting the builder's parsing logic.

If you must use orWhere, ensure that the raw SQL string is self-contained. The builder appends its own logic if it doesn't recognize the clause as fully resolved. A safer pattern for this specific "show all" requirement often involves checking the current state and resetting the query entirely when necessary, rather than layering complex boolean logic on top of existing scopes.

For instance, if the goal is to list both deleted and non-deleted items in a specific view, consider overriding the scope resolution order or explicitly disabling the withTrashed constraint for that specific request context. This avoids the complexity of constructing a "do nothing" condition via raw SQL, which is prone to the parsing errors seen when combining is null checks with dynamic filters.

Ultimately, while the intention behind the query was sound—retrieving every record regardless of deletion status—the execution failed due to how Laravel parses raw SQL fragments within boolean chains. By ensuring the raw block is a complete condition or by restructuring the scope definition, you can achieve the desired result without generating malformed SQL tokens.

Our network