Very Simple SQL Dump In Laravel

profile picture

Very Simple SQL Dump In Laravel

Sometimes when you're working on a specific feature, or maybe a specific page of your website, or even a unit test, you're wondering about the SQL queries that are executed.

Laravel Eloquent is great and all but it can be difficult to clearly see what query is executed. This is very important because it might help you to greatly improve the performance of your application.

A very simple way to display every request is possible through this small snippet.

DB::listen(function ($query) {
    dump($query->sql);
});

You can just paste this snippet at the top of your script and you're good to go!

This will obviously dump the query out on the page. You can choose to dump it on you terminal with beyondcode/laravel-dump-server

Another way would be to write all of that in your log file with the info() helper like this:

DB::listen(function ($query) {
    info($query->sql);
});

Happy debugging !

laravel
sql
dump
snippet

2019 My Dynamic Production SPRL All rights Reserved.