In last query in laravel, We have been many time require to get executed query log or you want to get last executed query or maybe if you want to display or Debug sql query from laravel Database query builder then you can do it that. therefore We have four Debugging example for display executed query in Laravel 5/6/7. when you are working on laravel application at that times , We think you require many time to print or debug last run query or if you want to check direct from phpmyadmin sql box, therefore at that time you have to step by step this four Debugging example. therefore, let’s follow four Debugging example of debug sql query from MySQL Database Query Builder.
Debugging Example 1 : print or debug last query in laravel eloquent
In this Debugging example we can get directly get current sql query using toSql() of MySQL Database query builder. In this Debugging example you don’t require to enable query log or something you can directly query like bellow example:
Ok, In this Debugging example we must require to enable query log using DB::enableQueryLog() of PHP MySQL Database Query builder. enableQueryLog() will receive to Log save or update all execute query in clear cache memory as well as we can print that query using DB::getQueryLog(). In this Debugging example you will get all query debug log not last only. bellow great example how to use it jointly.
To follow print last Executed MySQL query in Laravel version of the 5/6/7 we require to simple enable query log of Queries.So, First of all you To Enable query log use alternative Way to “DB::enableQueryLog()” method before debug query. As we recognize DB::getQueryLog() method of php return all executed MySQL queries.To print last query we will use end() method which return last executed query.
$employees= DB::select('select * from employee where 1');
$query = DB::getQueryLog();
$lastQuery = end($query);
Array ( [query] => select * from employee where 1 [bindings] => Array ( ) [time] => 0.41 )
How to get last query log in Laravel 5/6/7?
If you require to debug your last mysql query in laravel 5/6/7 then you can do it just by bellow multiple Debugging example. We recognize laravel provide their own Database query builder Therefore wherever we write Complex query 3 tables with 2 INNER JOIN, 1 subquery, 2 Group By on mysql Database query builder. But it’s give us error, warnning as well as you didn’t get it accurately then you require to run manually sql query to your Platform MySQL phpmyadmin.
here We will give you lots of the Debugging example to print your last query, Therefore let’s easy follow bellow Debugging example how it works.
I hope you get an idea about last query in laravel.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.