php – Laravel Eloquent display query log-ThrowExceptions

Exception or error:
use App\Order;

public function show(Order $order){

        $data = $order->all();
        return dd($order->getQueryLog());

Is there any way to display the query built by Eloquent in Laravel?

I tried getQueryLog(); but its not working

How to solve:

First you have to enable query log
it can be done using


then you can use below code to see the query log

$queries = DB::getQueryLog();

if you want to see the last executed query

$last_query = end($queries);

to know more about logging see this


public function show(Order $order){
        $data = $order->all();
        $queries = \DB::getQueryLog();
        return dd($queries);


To use getQueryLog() you need to enable it first:


If you want to see real queries, you can use Laravel Debugbar, it will show all real queries Laravel created during current request.

Sometimes ->toSql() is also useful.


Working on 5.6, something like this in AppServiceProvider::boot()

    // Log all DB SELECT statements
    // @codeCoverageIgnoreStart
    if (!app()->environment('testing') && config('app.log_sql')) {
        DB::listen(function ($query) {
            if (preg_match('/^select/', $query->sql)) {
                Log::info('sql: ' .  $query->sql);
                // Also available are $query->bindings and $query->time.

Then in config/app.php, just so it’s easy to enable/disable from amending the .env

    'log_sql' => env('LOG_SQL'),

All credit to:

And this can be parsed for unique queries with:

    grep ") sql:" laravel.log | sed -e "s#.*select\(.*\)\[\]#select\1#" | sort -u


To see the query logs in laravel.log file use the following way.

namespace App\Providers;

use DB;
use Log;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
     * Bootstrap any application services.
     * @return void
    public function boot()
        DB::listen(function($query) {

    // ...


I know it’s an old question, but it might help others that had the same issue I had.

If you use other connection than the default one, you should specify it to get the query log properly.

$test = MyModel::all();
$queries = \DB::connection('YourConnection')->getQueryLog();


You can use ::toSql() or ->toSql() as demonstrated below:

use App\Order;

public function show(Order $order){

    return $order::toSql();


use App\Order;

public function show(Order $order){

    return $order::where("id", "<>", 0)->toSql();

You might have to enable query log:


Leave a Reply

Your email address will not be published. Required fields are marked *