Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by whitelisting our website.

How to print query or debug queries in Laravel

We often need to debug large queries in Laravel. And laravel have built-in functions almost for everything. Here is a simple query in laravel for example.

$data = User::where(‘status’, 1)

->select(‘name’, ‘username’, ‘email’, ‘password’)

->get();

 

but we want to see the sql query. So we have to use toSql() in the place of get() and to print the query we use dd() – die and dump function of laravel, so here is out update query

$data = User::where(‘status’, 1)

->select(‘name’, ‘username’, ‘email’, ‘password’)

// ->get()

->toSql();

And dump it

dd($data);

and the output is

“SELECT ‘name’, ‘username’, ‘email’, ‘password’ FROM user WEHRE status = 1”

 

n.b.: it also can be done by DB listen

DB::listen(function($sql) {

var_dump($sql);

});

 

Here is another option with Laravel DebugBar. It’s a full featured package and gives lots on insight of the application.

 

 

——————————————————————

Reff: https://scotch.io/tutorials/debugging-queries-in-laravel

 

5 thoughts on “How to print query or debug queries in Laravel

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Wordpress Social Share Plugin powered by Ultimatelysocial

Remote PHP, Laravel, LAMP developer experience in Oracle Service Cloud

Hi, You can subscribe to get emails
about technology hacks, tools and many more.