How to get last executed mysql query in laravel 5 ?
In this post we will give you information about How to get last executed mysql query in laravel 5 ?. Hear we will give you detail about How to get last executed mysql query in laravel 5 ?And how to use it also give you demo for it if it is necessary.
In this post, i will tell you how to get last executed query in Laravel.
Sometime while working with laravel framework you need to see last executed query for debugging .
You can use DB::getQueryLog()
function to get all executed queries if you want to get last executed then use end()
method.
Before getting query log you need to first enable it by using DB::enableQueryLog()
function and then you can get all executed queries.
Example :
- public functionquerylog()
- {
- DB::enableQueryLog();
- $list=DB::table("categories")->get();
- $query=DB::getQueryLog();
- print_r(end($query));
- }
public function querylog() { DB::enableQueryLog(); $list = DB::table("categories")->get(); $query = DB::getQueryLog(); print_r(end($query)); }
Output :
Array ( [query] => select * from 'categories' [bindings] => Array ( ) [time] => 8.09 )
If there are multiple DB connections then you must specify it by following way :
First you will need to enable query log for your connection :
DB::connection('connection1')->enableQueryLog();
Now you can get log for that connection :
DB::connection('connection1')->getQueryLog()
By default query log is disabled in Laravel 5, so don’t forget to enable it before getting query log.
Hope this code and post will helped you for implement How to get last executed mysql query in laravel 5 ?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs