Friday, October 13, 2017

Laravel - eloquent - Logging All queries into logfile

Sometimes you need to know the SQL interpretation of your eloquent query to check performance or even check the SQL query syntax is valid , you may try a laravel debugging package like Laravel-Debugbar, But whilst this package is great , it still cannot be good tool when it comes to SPA (single page application) which run tens of requests  , but this package only returns information about each request , which means the next request will write over the previous request debugging information.

So , in this case we need to log all request information , specially the SQL queries to a log file using the Example mentioned in Laravel documentation.

<?php

namespace App\Providers;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        DB::listen(function ($query) {
            // $query->sql
            // $query->bindings
            // $query->time
        });
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

No comments:

Post a Comment