'; }Step 2. groupBy with Raw ConditionNext, what if you want to group by birth year?
Here’s the visual result for my dummy seeded data:Step 3. groupBy Raw with Boolean ConditionNow we’re getting to the “trick” of the article.
How to group by condition of “born before or after 2001 year”?
Here’s the result view in Blade: @foreach ($results as $result) @endforeach
Adult? Amount
{{ $result->adult == 1 ?
'Yes' : 'No' }}
{{ $result->amount }}
Here’s how it looks visually:You can use such grouping whenever you have clear condition of TRUE/FALSE, like odd/even numbers somewhere, some number is over/under etc.

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Community Partners