In addition to @delmadord's answer and your comments:
There is currently no way to create a subquery in FROM , so you need to manually use the raw command, and then, if necessary, you merge all the bindings:
$sub = Abc::where(..)->groupBy(..); // Eloquent Builder instance $count = DB::table( DB::raw("({$sub->toSql()}) as sub") ) ->mergeBindings($sub->getQuery()) // you need to get underlying Query Builder ->count();
Remember that you need to combine the bindings in the correct order . If you have other related sentences, you should put them after mergeBindings :
$count = DB::table( DB::raw("({$sub->toSql()}) as sub") ) // ->where(..) wrong ->mergeBindings($sub->getQuery()) // you need to get underlying Query Builder // ->where(..) correct ->count();
Jarek Tkaczyk Jul 19 '14 at 8:29 2014-07-19 08:29
source share