Database
SQL
How To Display Query Log In Laravel 7/6?
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.
- TAGS
- Query
- 4.5/5.0
- Last updated 08 September, 2022
- By Admin
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...
Categories
PHP
(5)
Codeigniter
(5)
Laravel
(3)
JS
(1)
Angular JS
(5)
Node JS
(5)
Vue JS
(4)
React JS
(6)
jQuery
(5)
Javascript
(3)
jQuery UI
(6)
Wordpress
(1)
Python
(5)
Database
(1)
MySql
(5)
Mongo DB
(1)
DBMS
(1)
SQL
(5)
HTML
(1)
HTML 5
(5)
CSS
(5)
Bootstrap
(5)
Ajax
(5)
JAVA
(1)
JSON
(5)
Ubuntu
(5)
Server
(5)
Git
(3)
Google
(1)
Google Map
(7)
Google API
(1)
Elastic Search
(1)
Apache
(1)
Installation
(1)