use laravel ORM
From SQl to Laravel Query builder
I have this SQL query but had no idea how to write it in Laravel Query Builder
select state_id, state_name, sum(inactifs) as inactifs, sum(actifs) as actifs,
sum(inactifs) + sum(actifs) as total
from
(
select distinct s.id as state_id, s.name as state_name, u.id as user_id,
case when uga.id is null then 1 else 0 end as inactifs,
case when uga.id is null then 0 else 1 end as actifs
from users u
inner join states s on u.state_id = s.id
left join user_group_affiliations uga on uga.user_id = u.id
and (uga.active = 1 and (uga.start_date is null or uga.start_date <= now()) and (uga.end_date is null or uga.end_date >= now()))
) quer group by state_id;
Practice writing these things and understand it takes some trial and error sometimes.
So many existing conversion examples given already I recently gave a couple myself.
https://laravel.com/docs/5.8/queries
https://laracasts.com/discuss/channels/eloquent/convert-raw-sql-to-eloquent
You could also use your working query as is. Laravel converts right back to normal SQL at runtime.
no idea how to write it in Laravel Query Builder
That's where you practice the examples given in the documentation and view some of the free video series already referenced in the link I gave in the other post.
Please or to participate in this conversation.