Monday, April 3, 2017

How can I write the query as raw in Laravel?

Here is my query:

SELECT rid
FROM relations
WHERE rootid IN (736, 781) 
GROUP BY rid HAVING COUNT(rid) = 2");

I'm trying to implement that in Laravel. How can I do that? (both "query builder and eloquent approaches are fine to me")


This doesn't work: (it throws "something went wrong")

DB::table('relations')
->select(DB::raw('count(*) as user_count, status'))
->whereRaw('rootid IN (736, 781)')
->groupBy('rid')
->havingRaw('COUNT(rid) = 2')
->get();



via stack

Advertisement