Search code examples
laravellaravel-4eloquentcompiled-query

laravel Eloquent ORM - How to get compiled query?


In Laravel 4.2 I want to get Compiled Query.

This is what i have:

$product = Product::where('id', '=', '100')->get();

I want compiled query like:

select * from products where id = 100 

Purpose of the question is: i want to use it as sub query in another query.

I have searched and found Class Grammer and Class MySQL But i did not found solution for that.

Is there any solution?

Your help would be appreciated.


Solution

  • You can use grammer for subqueries, See below example for reference :

    $users = DB::table('users')
                 ->where('user.id', '=', $userID)
                 ->join('product', 'product.userid',  '=', 'user.id');
    
    $price = $users->select(array(DB::raw('SUM(price)')))
                   ->grammar
                   ->select($users); // Compiles the statement
    
    $result = DB::table('users')->select(array(DB::raw("({$price}) as price)))->get();
    

    This add a subquery to your main query.