Posts Learn Components Snippets Categories Tags Tools About
/

How to get query builder to output its raw SQL query as a string in Laravel?

Learn how to get the raw SQL string from a query builder in Laravel the easy way. Get to know this information to inspect the query used to retrieve the data.

Created on Sep 23, 2021

3767 views

In this short snippet, you will learn how to get the raw SQL string from a query builder in Laravel. To get the raw query you can make use of the "toSql()" available from the "QueryBuilder" instance.

Method 1: Get raw SQL using toSql()

<?php

DB::table('posts')->toSql()
The query above will return the following raw SQL.
select * from `posts`
The method above is the easiest way to get the raw SQL besides using the event listener and another advantage of using it is that you can look at the query at any point while building the query.
Do note that this method works for Query Builder or Eloquent, however toSql() is used instead of first() or get(). You cannot run the query and also get the SQL at the same time using this method.

Method 2: Get raw SQL using Query Log


The second method is to use the "enableQueryLog()" method of the "DB" facade and it looks as follows.
<?php

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

// Write your query here

dd(DB::getQueryLog()); // Show results of log
The result will be as follows, the recent query will be at the bottom
array(1) {
  [0]=>
  array(3) {
    ["query"]=>
    string(21) "select * from "users""
    ["bindings"]=>
    array(0) {
    }
    ["time"]=>
    string(4) "0.92"
  }
}

Method 3: Using Event Listener


The third method is to listen to the "illuminate.query" event and you can define it like below.
<?php

Event::listen('illuminate.query', function($query, $params, $time, $conn) 
{ 
    dd(array($query, $params, $time, $conn));
});

Method 4: Using dump method


For the fourth method, you can make use of the "dump" method to chain the query. This method is available as of Laravel 5.8.15 and the query builder now has dd and dump methods.
<?php

DB::table('posts')->where('slug', 'hello-world')->dump();

Method 5: Using Debugbar Extension


The fifth method is to use the Laravel Debugbar. This extension is a package that can be installed through the composer package manager.
composer require barryvdh/laravel-debugbar
Upon installing do visit the "queries" tab and you will be able to see the raw queries.

Other Mentions


One other way is to make use of Laravel Clockwork and which is similar to Laravel Debugbar. You can preview your raw SQL query from within the debug page.

Conclusion


By now you should be able to get the raw SQL query from Laravel Query Builder. If you find this snippet to be helpful do make sure to share it with your friends and cheers. If you have any feedback do start a new discussion from the comment box below.

If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️

new

PostSrc Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that are right for your project.

View Components

Sponsors 👑

+ Add Yours
)