use App\Order; public function show(Order $order) { $data = $order->all(); return dd($order->getQueryLog());
Is there any way to display the query built by Eloquent in Laravel?
I tried getQueryLog();
but its not working
You can use simple function that is available in Laravel. Use the enableQueryLog method to enable query log in Laravel DB::connection()->enableQueryLog(); You can get array of the executed queries by using getQueryLog method: $queries = DB::getQueryLog(); answered Mar 23, 2020 by Niroj.
Using laravel query log we can print the entire query with params as well. In this type of debugging query is executed and we will get the complete parsed query. Here we used DB::enableQueryLog to enable the query log and DB::getQueryLog() to print the all queries in between of it.
you can simply print last eloquent query in laravel 7/6. I will print last sql query in laravel 7 using toSql(), DB::enableQueryLog() and DB::getQueryLog(). i will also show you output of print sql query. So, let's see examples bellow and use as you want any one.
First you have to enable query log it can be done using
DB::connection()->enableQueryLog();
then you can use below code to see the query log
$queries = DB::getQueryLog();
if you want to see the last executed query
$last_query = end($queries);
to know more about logging see this https://laravel.com/docs/5.0/database#query-logging
Example
public function show(Order $order){ \DB::connection()->enableQueryLog(); $data = $order->all(); $queries = \DB::getQueryLog(); return dd($queries); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With