ItSolutionStuff.com

How to display query log in Laravel 7/6?

By Hardik Savani • October 12, 2023
Laravel SQL

Sometime we need to print last executed query in laravel 7/6 application for debug. you want to see what last query run. i will give examples of how to print query login in laravel 7/6. you can simply print last eloquent query in laravel 7/6.

I will print last sql query in laravel 7 using toSql(), DB::enableQueryLog() and DB::getQueryLog(). i will also show you output of print sql query.

So, let's see examples bellow and use as you want any one.

Example 1:

Controller Code:

$query = User::select("*")->toSql();

dd($query);

Output:

select * from `users`

Example 2:

Controller Code:

DB::enableQueryLog();

$users = User::select("*")->get();

$quries = DB::getQueryLog();

dd($quries);

Output:

array:1 [▼

0 => array:3 [▼

"query" => "select * from `users`"

"bindings" => []

"time" => 4.25

]

]

Example 3:

Controller Code:

DB::enableQueryLog();

$users = User::select("*")->get();

$query = DB::getQueryLog();

$query = end($query);

dd($query);

Output:

array:3 [▼

"query" => "select * from `users`"

"bindings" => []

"time" => 2.07

]

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel 5.6 - Log viewer using LogViewer package example

Read Now →

Laravel Custom User Log Activity Example Tutorial

Read Now →

How to Clear Log File using Command in Laravel?

Read Now →

Laravel Select with Count Query with Group By Example

Read Now →

How to Concat Two Columns in Laravel?

Read Now →

Laravel Eloquent Where Like Query Example Tutorial

Read Now →

Example of unionAll in Query Builder Laravel

Read Now →

Laravel Join with Subquery in Query Builder Example

Read Now →

How to Get Query Log in Laravel Eloquent?

Read Now →