Laravel query builder with AND in query

Laravel query builder with AND in query

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

Stefan Bogdanescu

Founder & Senior Architect

Adding AND Conditions to Laravel Query Builder Subqueries

When working with complex filtering logic in Laravel, you often need to construct dynamic SQL conditions inside closure-based subqueries. A common pattern involves chaining multiple where clauses together using or_where to handle partial matches across several columns. For instance, a search feature might check if a term exists in an id, name, or status field.

$orders = DB::table('address')->where(function($query) use ($term) {
    $query->where('id', 'LIKE', '%' . $term . '%')
          ->or_where('name', 'LIKE', '%' . $term . '%')
          ->or_where('status', 'LIKE', '%' . $term . '%')
          // and_clause_goes_here('is_orderer', '=', '1');
});

In the example above, the first three conditions form an OR group. However, you frequently need to apply a strict requirement that must accompany this entire group. In standard SQL, you would wrap these conditions in parentheses and append an AND clause at the end: (id LIKE '%term%' OR name LIKE '%term%' OR status LIKE '%term%') AND is_orderer = 1.

Laravel's Query Builder handles this grouping automatically for the chained where/or_where calls, but it does not provide a direct method like and_clause_goes_here() to append logic outside that group. Attempting to force a string injection or assuming a missing public method will lead to confusion. To achieve the desired SQL structure, you must use the andWhere method before entering the closure if you want the condition to apply to everything inside, or handle the grouping manually within the closure itself.

If your goal is to ensure that every row returned by the subquery satisfies a specific condition (like is_orderer = 1) regardless of the partial match logic, you simply add an andWhere call before entering the closure or after the closure if you are chaining further conditions on the main query.

$orders = DB::table('address')
    ->where(function($query) use ($term) {
        $query->where('id', 'LIKE', '%' . $term . '%')
              ->orWhere('name', 'LIKE', '%' . $term . '%')
              ->orWhere('status', 'LIKE', '%' . $term . '%');
    })
    ->andWhere('is_orderer', '=', 1)
    ->paginate($per_page);

This approach generates SQL equivalent to: WHERE (id LIKE ... OR name LIKE ... OR status LIKE ...) AND is_orderer = 1. The key here is understanding that where closures create a distinct group. Any condition added via andWhere on the main query object applies to the entire result set of that closure, effectively acting as the external AND operator you were looking for.

It is crucial to distinguish between logical precedence within the closure and outside it. Inside a where callback, all chained methods belong to that specific group. If you need to combine an OR block with an AND condition strictly inside that same closure, you must manually manage the parentheses logic or use the orWhereRaw / andWhereRaw methods to inject raw SQL segments that explicitly handle the grouping syntax.

$orders = DB::table('address')->where(function($query) use ($term) {
    $query->where(function($innerQuery) use ($term) {
        $innerQuery->where('id', 'LIKE', '%' . $term . '%')
                   ->orWhere('name', 'LIKE', '%' . $term . '%')
                   ->orWhere('status', 'LIKE', '%' . $term . '%');
    })
    ->andWhere('is_orderer', '=', 1);
});

While nesting closures is an option, the simpler solution in most cases is to place the strict filtering condition outside the closure entirely. This ensures the AND logic applies to the whole group without requiring manual parenthesis manipulation. If you require more complex nested grouping inside a single closure, using raw SQL expressions via whereRaw with explicit parentheses offers full control over the generated query structure.

For developers seeking further examples on constructing intricate query scopes or handling dynamic filtering patterns in Laravel, resources like https://laravelcompany.com can provide additional context on best practices for maintaining clean and readable database queries as applications scale.

Our network