Print last query in laravel eloquent – Examples

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:

$employee = Employee::where('id',5)->toSql();

print last query in laravel eloquent,last query in laravel 6,laravel 5 eloquent print query,laravel db query
How to Get Last Executed Query in Laravel

Debugging Example 2 : dd last query laravel

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.

$employee = Employee::get();
$query = DB::getQueryLog();

Debugging Example 3 : db query

In this Debugging example We make the same as Debugging example 2, but you can get exactly last query using end(). therefore let’s follow how to use.

$employee = Employee::get();
$query = DB::getQueryLog();
$query = end($query);

Echo Last Executed Query In Laravel

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.

Also Read This ๐Ÿ‘‰   User Profile Edit Script Update Script Laravel 7/6

$query = DB::getQueryLog();
$lastQuery = end($query);

dd last query laravel,print query in laravel,laravel benchmark queries,laravel return raw query
last query in laravel


Here is simple Debugging example.

$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.

Debug Way 1: laravel tosql with bindings

$lastQuery = DB::table("employees")->toSql();


Debug Way 2: laravel db::query


$employee = DB::table("employees")->get();

$lastQuery = DB::getQueryLog();


Debug Way 3: laravel db::listen

$employee = DB::table("employees")->get();
$lastQuery = DB::getQueryLog();
$lastQuery = end($lastQuery);


How to get last executed mysql query in laravel 5/6/7 ?

In this Example, We will show you how to print last executed query in Laravel 5/6/7. you can print last executed query using enableQueryLog() and getQueryLog() in PHP framework.

You can use DB::getQueryLog() method to print all executed queries.If you want to print the last executed query then use end().

Before debug query log you require to first of all enable it by using method and then you can print all executed queries.

Example 1: Debugging Queries in Laravel

  public function lastQuery()

        $list = \DB::table("employees")->get();
        $query = \DB::getQueryLog();



array:3 [
  "query" => "select * from `employees`"
  "bindings" => []
  "time" => 6.96

If there are multiple DB connections in PHP framework then you must identify it by bellow way:

Now you can get log for that connection :

Also Read This ๐Ÿ‘‰   php lowercase - The Best PHP 3 Examples


Web Programming Tutorials Example with Demo

Read :


You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about last query in laravel.
I would like to have feedback on my 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.