Laravel Debugging Raw SQL Queries result

Today, We want to share with you Laravel Debugging Raw SQL Queries result.In this post we will show you DB::getQueryLog() Returning Empty Array, hear for call to a member function tosql() on array we will give you demo and example for implement.In this post, we will learn about laravel get sql query with parameters with an example.

Laravel Debugging Raw SQL Queries result

There are the Following The simple About Laravel 6 How to log all queries on a page? Full Information With Example and source code.

As I will cover this Post with live Working example to develop laravel 6 eloquent print query, so the Get the Query Executed in Laravel 6 is used for this example is following below.

OUTPUT THE RAW SQL QUERY IN LARAVEL 6

Getting raw sql query output from query builder

Read Also:  How To Print Last Query In PHP?

How Do I Get the Query Builder to Output Its Raw SQL Query as a String?

DB::table('products')->get();

//SQL Query
SELECT * FROM products

DB::table('products')->toSql() 

 DB::getQueryLog()

DB::listen(function ($query) {

// $query->sql
 // $query->bindings
// $query->time

 });

Laravel 6 Debugbar Package

Another way to viable option is to install the step by step Laravel 6 Debugbar package. and then u can installed you will fetch a heads up overview of your app that is displayed at the bottom of the browser. It’s a very easy full-featured package and will give you more of insights into your laravel 6 web application.

To output to the screen the last queries ran you can use this:

// Enable query log
DB::enableQueryLog(); 

// Your Laravel Model Eloquent query executed by using get()

// Display results of log
dd(DB::getQueryLog());

Debugging Queries in Laravel

$results = Member::where(function($q) use ($request) {
    $q->orWhere('email', 'like', '%[email protected]%');
    $q->orWhere('address', 'like', '%usa%');
    $q->orWhere('city', 'like', '%dhaba%');
})->get();

Simple Query Debugging

$results = Member::where(function($q) use ($request) {
    $q->orWhere('email', 'like', '%[email protected]%');
    $q->orWhere('address', 'like', '%surat%');
    $q->orWhere('city', 'like', '%kora%');
})->toSql();
dd($results)

Listening For Query Events in Laravel

\DB::listen(function($sql) {
    var_dump($sql);
});

//or

\DB::listen(function($sql, $bindings, $time) {
    var_dump($sql);
    var_dump($bindings);
    var_dump($time);
});

How to print query or debug queries in Laravel 6

$data = Member::where('is_active', 1)
->select('member_name', 'address', 'city', ‘password’)
->get();

$data = Member::where('is_active', 1)
->select('member_name', 'address', 'city', ‘password’)
// ->get()
->toSql();
dd($data);

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

Read Also:  jQuery Regular Expression Real Time Form Validation Example

using DB listen

DB::listen(function($sql) {

var_dump($sql);

});

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Laravel 6 Eloquent display query log.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.