Search code examples
phpsqllaravellaravel-4

How can I get the raw query string from Laravel's query builder BEFORE executing the query?


I have a complex query created by a few conditions, and I would like to get the final SQL query from the builder object is about to execute. Can I do that?


Solution

  • You can get it doing:

    $query = DB::table('brands')
                    ->join('products','a','=','c')
                    ->whereNull('whatever');
    
    echo $query->toSql();
    

    But Laravel will not show you parameters in your query, because they are bound after preparation of the query.

    So you can also do:

    print_r( $query->getBindings() );