laravel print last query | Executed | Sql | php | tutorials

In this article, We will know you how to get and print last query in laravel eloquent.

manytimes while working with laravel any project php framework you required to print last executed query for source code tetsing with debugging .

print last query in laravel

You can use laravel DB::getQueryLog() method to get all an executed sql queries if you want to get last executed then use end() method.

print last query in laravel eloquent,last query in laravel 6,laravel 5 eloquent print query,laravel db query,dd last query laravel,print query in laravel,laravel benchmark queries,laravel return raw query
Print last query in laravel eloquent

Before printing sql query log you required to first of all the enable it by using simple DB::enableQueryLog() method as well as then you can get all executed sql queries using Last Query In Laravel.

Also Read This ๐Ÿ‘‰   Multiple Image Upload using PHP into MYSQL database

dd last query laravel

public function querylog()
        $list = \DB::table("products")->get();
        $query = \DB::getQueryLog();


  Array ( [query] => select * from `products` [bindings] => Array ( ) [time] => 8.09 )

Example 1: laravel 5.6 get last query

In this laravel script we can get directly get current sql query using toSql() of laravel query builder. In this laravel script you don’t need to enable query log or something you can directly query like bellow laravel script:

$product = Product::where('is_active',1)->toSql();

Example 2: laravel 5 eloquent print query

Ok, In this laravel script we must need to enable query log using DB::enableQueryLog() of Laravel Query builder. enableQueryLog() will give access to store all execute query in cache and we can get that query using DB::getQueryLog(). In this laravel script you will get all query log not last only. bellow laravel script how to use it together.

$product = Product::get();
$sqlQQ = DB::getQueryLog();

Example 3: laravel db query

In this laravel script We create the same as source code 2, but you can get exactly last query using end(). therefor let’s display how to use.

$product = Product::get();
$sqlQQ = DB::getQueryLog();
$sqlQQ = end($sqlQQ);

I hope you get an idea about print 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.