Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to select from subquery using Laravel Query Builder?

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 7.74k
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: