Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Eloquent display query log

Tags:

php

laravel

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

like image 694
Benjamin W Avatar asked Dec 14 '16 11:12

Benjamin W


People also ask

What is enableQueryLog in laravel?

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.

How do I print a query in laravel 8?

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.

How do I print a query in laravel 7?

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.


1 Answers

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); } 
like image 124
kapil.dev Avatar answered Oct 20 '22 17:10

kapil.dev