How to see the queries that were executed by the eloquent in Laravel?

Asked

Viewed 3,070 times

4

How can I find out which queries were executed in Laravel?

For example, I have the following query:

$usuarios = Usuario::with('nivel')->where(['status' => 1])->get();

I would like to know which queries were executed. You can log this in Laravel?

I would like to include Laravel 5 and 4 in the answers, as I usually use both.

  • Try putting ...->toSql() instead of ...->get(). Is that?

  • But this will not make the query. This will convert the query to Model in an SQL string. I want to continue running the Get() and discover all queries that were executed on the pages.

  • I’ve never done that, and I don’t want to risk responding without testing. See if this helps? http://stackoverflow.com/questions/27753868/how-to-get-the-query-executed-in-laravel-5-dbgetquerylog-returning-empty-arr , http://stackoverflow.com/questions/14536165/get-the-query-executed-in-laravel-3-4

  • It works, Miguel. I asked this question to give an opportunity for someone to answer it. I already knew the answer. You should run DB::enableQueryLog before the queries. Hence, from the query you want to capture the log, you call him before. Then at the end of all queries Voce calls getQueryLog to capture them.

  • You may answer, young man!

2 answers

4


In Laravel 4 can do:

$queries = DB::getQueryLog(); // array de todas as queries que foram corridas

Already on Laravel 5, since the log is not active by default and we have to activate it:

use DB;
...
DB::enableQueryLog();
...
$queries = DB::getQueryLog(); // array de todas as queries que foram corridas

Still in Laravel 5, if you have multiple databases and only want to activate the log for one of them (one of the connections):

DB::connection('connection')->enableQueryLog();

Where to place this activation?

This can be put in a middleware that goes into action on some/some requests, so that everything that is done (queries in this case) in the rest of the request is saved, and finally we can use the method terminate to recover stored queries:

class AntesDeQualquerQuery
{
    public function handle($request, Closure $next)
    {
        DB::enableQueryLog();
        return $next($request);
    }

    public function terminate($request, $response)
    {
        // dump de todas as queries no termino de execução deste middleware
        dd(DB::getQueryLog());
    }
}

We can also put this in the file bootstrap/app.php, so each time Artisan (CLI) comes into action this is set right away:

$app['events']->listen('artisan.start', function(){
    \DB::enableQueryLog();
});

In case we need the logo only on site and not in production:

if (App::environment('local')) {
    // Se for local queremos ativar o log
    DB::enableQueryLog();
}

DOCUMENTATION

  • Congratulations, young man. You have chosen the answer :D +1

  • An addendum: In Laravel 4, you do not need to use the use DB, since you don’t have namespace.

  • It was a bit adapted from one that seemed good from Soen. Obgado @Wallacemaxters

  • @Wallacemaxters depends, nothing prevents the person to use namespaces in their code.

  • Yes @gmsantos, but by default Laravel 4 does not use, I think it was not worth scaling the answer to that point

  • @gmsantos yes, it will depend on the person wanting to use yes. I have already used in an application in Laravel 4, but it is very rare.

  • @Wallacemaxters for the record, I think it’s a good question myself I will use this log whenever necessary. I already spent my vows today but tomorrow I’ll put +1 in the question I think is useful for those who use Laravel

  • @Miguel is that the way it was placed gives the impression that the Laravel 4 does not work with namespaces, which is not true.

  • Sure @gmsantos , maybe the framework by default doesn’t use, but if php allows then can ;)

Show 4 more comments

2

Another way to see the darlings executed by a request directly in the browse is through the Laravel-debugbar.

It is compatible with Laravel 4 and 5, and has a tab that shows all darlings. It even has a configuration that can also include the Execution plan of that query.

Laravel Debugbar

Browser other questions tagged

You are not signed in. Login or sign up in order to post.