How to Log Query in Laravel

Are you looking to print the last query in Laravel? Or in other words, we can say do you want to log query in Laravel? Both questions have slightly different answers. For printing the last query, we can just print the last executed query and display it on a browser. But we recommend a way of logging queries in Laravel. Doing so, our all queries will save in the log file.

In this article, we show you both options which help a user to view the last executed Laravel query.

When we are developing a Laravel application, sometimes we came across a situation where we need to see if we wrote a correct query or not. This is because of several reasons – maybe we are not getting an expected output, we are getting a slow response from a database server, we do not get any output as something wrong with our query, etc.

How to Print Last Query in Laravel

Printing last executed query is a one time process. It just prints your query to the browser. It does not store the query anywhere. Below is a simple example to print the last query in Laravel.

DB::enableQueryLog();
$arr_user = DB::table('users')->select('name', 'email as user_email')->get();
dd(DB::getQueryLog());

Here we have used two statements DB::enableQueryLog() and dd(DB::getQueryLog()). The first statement enables the query logging, while the second statement actually prints this log on the browser. Of course, this will work with Laravel Eloquent also.

One can use this way if they don’t want to log queries. But as said this is not actually a convenient way. Because, if you follow this technique then you need to repeat these two statements for every query.

Log Query in Laravel

We recommend a technique of logging queries in Laravel. It is a more easy and superior way to log all queries. You don’t need to go at the place of each query and add the code.

Open the app/Providers/AppServiceProvider.php file, and add 2 Facadaes for File and DB as follows.

use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\DB;

Next, in the boot() method we will write a below code which logs all your Laravel queries in the background.

public function boot()
{
    if(env('APP_DEBUG')) {
        DB::listen(function($query) {
            File::append(
                storage_path('/logs/query.log'),
                $query->sql . ' [' . implode(', ', $query->bindings) . ']' . PHP_EOL
           );
        });
    }
}

Now, whenever query executed it will log in the storage/logs/query.log file and you can directly view the queries from this log file.

In Laravel, one more way is available to log the queries. By this way your query logs get stored in storage/logs/laravel.log file. To do so, user need to place the below code in routes/web.php.

\DB::listen(function($sql) {
    \Log::info($sql->sql);
    \Log::info($sql->bindings);
    \Log::info($sql->time);
});

We recommend using either one of the above methods for logging the queries. A user doesn’t need to use both methods at a time.

Related Articles

If you liked this article, then please subscribe to our Youtube Channel for video tutorials.

Leave a Reply

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