Laravel print or get last executed query; In this tutorial, we will learn how to print or get last executed query in laravel apps.
How to Print or Get Last Executed Query in Laravel
Use the following methods to print or get last executed query in laravel apps:
- Solution 1 – Laravel Last Query using toSQL()
- Solution 2 – Laravel Get Last Executed MySQL Query
Solution 1 – Laravel Last Query using toSQL()
Use the following query to get the current SQL query you can do it with Laravel query builder’s toSql()
method; as follows:
$query = User::select("*")->toSql(); dd($query);
Solution 2 – Laravel Get Last Executed MySQL Query
Before get last executed mysql query to enable the query log in Laravel app using query builder’s DB::enableQueryLog() method. Then use enableQueryLog() method to print last executed mysql query in laravel apps; as follows:
DB::enableQueryLog(); $users = User::select("*")->get(); $quries = DB::getQueryLog(); dd($quries);