Sub query is one the important section of database query.There are n number of situation where we want to implement sub query to fetch the data from the table. In Laravel 4.x there is no specific keyword to perform any
sub query.To perform sub query In our code we will use mergeBindings.
The example of subquery using laravel 4.x is
$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();
Mind that you need to merge bindings in correct order. If you have other bound clauses, you must put them after mergeBindings:
By this way we can use sub query in our Laravel 4.x project.
1 Comment(s)