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.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

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.

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.

Related FAQ

Here are some more FAQ related to this Article:

  1.   Simple Get Category ID using Category Name
  2.   vue.js Dynamic Dependent Dropdown using PHP
  3.   PHP Laravel Redirect To Another URL without http of Other site
  4.   WooCommerce Get Product Images
  5.   Working with datetime in PHP Examples
  6.   Simple Jquery Search Filter Example
  7.   PHP Laravel get months between two dates
  8.   vue Select Box binding Example
  9.   Laravel 6 Join multiple tables Eloquent Examples
  10.   Laravel 6 Joins Example Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *