Skip to content

Grouped Conditions

Gilberto Junior edited this page Nov 29, 2019 · 3 revisions

Syntax

function where(Closure $conditions): self;
function and(Closure $conditions): self;
function or(Closure $conditions): self;

Example

PHP's way:

$repository
    ->where(function ($repository) {
        return $repository->gte('age', 18)
           ->isNull('canceled_at');
    })
    ->or(function ($repository) {
        return $repository->lte('size', 100)
           ->equals('active', 1);
    })
    ->and(function ($repository) {
        return $repository->between('birth_date', '1980-01-01', '1999-12-31');
    });

SQL's representation:

WHERE (age >= 18 AND canceled_at IS NULL) OR (size <= 100 AND active = 1) 
  AND (birth_date BETWEEN '1980-01-01' AND '1999-12-31');

🡄 Is Null Or Not | Group By 🡆