onlinecode

How to display query log in Laravel 7/6?

How to display query log in Laravel 7/6?

In this post we will give you information about How to display query log in Laravel 7/6?. Hear we will give you detail about How to display query log in Laravel 7/6?And how to use it also give you demo for it if it is necessary.

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:

Also see:How to Create and Use Query Scope in Laravel Eloquent

array:3 [▼

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

"bindings" => []

"time" => 2.07

]

I hope it can help you…

Hope this code and post will helped you for implement How to display query log in Laravel 7/6?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Exit mobile version