J D
J D

Reputation: 1818

SQL to Eloquent ORM

I have a SQL query which I wish to use in my Laravel application. The SQL query is like:

SELECT status, count(status) AS num
FROM event_businesses
WHERE event_id = ?
GROUP BY status
ORDER BY status ASC

and what I have till now is

$event_businesses = EventBusiness::select('status')
                    ->where('event_id', '=', $event_id)
                    ->groupBy('status')
                    ->orderBy('status', 'asc')->get();

I really don't get where can I add the count(status) as num aggregate to my ORM query.

Thanks in advance!

Upvotes: 1

Views: 1852

Answers (1)

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87789

You might need to do this:

$event_businesses = EventBusiness::select(DB::raw('status as status, count(status) as count'))
                    ->where('event_id', '=', $event_id)
                    ->groupBy('status')
                    ->orderBy('status', 'asc')->get();

Upvotes: 1

Related Questions