DEV Community

Ankit Verma
Ankit Verma

Posted on

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

In this blog, we'll explore how to retrieve the raw SQL query from Laravel's Query Builder with practical examples.

Why Would You Need the Raw SQL Query?

Understanding the raw SQL query can be beneficial in several cases:

  • Debugging: If a query isn't returning expected results, inspecting the SQL statement can help identify issues.
  • Logging: Storing queries for later analysis or monitoring query performance.
  • Optimization: Reviewing the SQL query can help improve efficiency by adding indexes or restructuring queries.

Getting the Raw SQL Query in Laravel

Laravel provides a convenient method to retrieve the SQL query with bindings. Below are a few ways to achieve this:

1. Using toSql() Method

The toSql() method returns the raw SQL query as a string without executing it.

Example:

$query = DB::table('users')->where('status', 'active');

// Get raw SQL query
$sql = $query->toSql();

echo $sql;
Enter fullscreen mode Exit fullscreen mode

Output:

select * from `users` where `status` = ?
Enter fullscreen mode Exit fullscreen mode

Note: The ? represents a bound parameter, and the actual value isn’t displayed.

2. Getting the Query with Bindings

To get the query with values instead of placeholders, you need to manually replace bindings:

$query = DB::table('users')->where('status', 'active');

$sql = vsprintf(str_replace('?', '%s', $query->toSql()), array_map('addslashes', $query->getBindings()));

echo $sql;
Enter fullscreen mode Exit fullscreen mode

Output:

select * from `users` where `status` = 'active'
Enter fullscreen mode Exit fullscreen mode

3. Logging Queries Using Query Listener

Laravel provides a way to listen to executed queries using DB::listen(). This is useful for debugging queries dynamically:

DB::listen(function ($query) {

    echo $query->sql;
    print_r($query->bindings);
});
Enter fullscreen mode Exit fullscreen mode

This approach captures and logs every executed query in real time.

4. Using Laravel Debugbar (For Debugging in Development)

If you're using the Laravel Debugbar package, you can easily see all executed queries:

composer require barryvdh/laravel-debugbar --dev
Enter fullscreen mode Exit fullscreen mode

Once installed, all queries will be logged in the Debugbar UI.

The toSql() method provides a quick way to see the SQL structure, while manually replacing bindings can reveal the full executed query. Additionally, using query listeners or debugging tools like Laravel Debugbar can help track executed queries in real time.

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more