Wednesday, February 26, 2020

How to show Log Query in Laravel 6?

How to show Log Query in Laravel 6?

At some point we have to print the last query executed in the Laravel 6 application to debug it. You want to see which last query was run. I will give examples of how to print the login session in Laravel 6. You can simply print the last eloquent query in Laravel 6.

I will print the last SQL query in Laravel 6 with toSql (), DB :: enableQueryLog () and DB :: getQueryLog (). I will also show you the output of the SQL print query.

So let's look at the following examples and use them as you wish.

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 This Could help you ...

0 comments:

Post a Comment

Please don't enter any spam link in the comment box.