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();
dd($employee);
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.

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

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.

DB::enableQueryLog();
$employee = Employee::get();
$query = DB::getQueryLog();
$query = end($query);
dd($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.

$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

Example

Here is simple Debugging example.

DB::enableQueryLog();
$employees= DB::select('select * from employee where 1');
$query = DB::getQueryLog();
$lastQuery = end($query);
dump($lastQuery);

Output

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();

dump($lastQuery);

Debug Way 2: laravel db::query

DB::enableQueryLog();

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

$lastQuery = DB::getQueryLog();

dump($lastQuery);

Debug Way 3: laravel db::listen

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

dump($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()
    {
        \DB::enableQueryLog();

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

        dd(end($query));
    }

Output

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:

 \DB::connection('connection_name')->enableQueryLog();

Now you can get log for that connection :

 \DB::connection('connection_name')->getQueryLog()
Web Programming Tutorials Example with Demo

Read :

Summary

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

Leave a Comment