How can I get the raw query string from Laravel's query builder BEFORE executing the query?
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: How to Retrieve the Raw Query String from Laravel's Query Builder Before Execution
Introduction: As we build complex queries using Laravel's query builder, it is often useful to have access to the raw SQL statement for debugging or other purposes. In this comprehensive blog post, we will explain how you can get the final query from the builder object before executing the query.
Body:
1. Understanding Laravel's Query Builder:
Laravel provides a powerful query builder that allows developers to construct queries easily using fluent syntax. It is an essential tool for data manipulation, and it helps abstract away the complexity of writing raw SQL statements. However, sometimes you may need to access or modify the generated SQL before executing the actual query.
2. Getting the Raw Query Without Modifying the Statement:
To access the raw query, we can use a simple approach that does not interfere with the building process:
```php
$queryBuilder = DB::table('users');
$rawQuery = (string) $queryBuilder->toSql(); // cast to string for SQL output
echo $rawQuery; // prints the final query
```
Using the above code, we can get the raw query from the builder object before executing the actual query. For this method, we use the `toSql()` function, which returns a string representation of the query being built by Laravel's query builder. However, note that any changes made to the builder after calling `toSql()` will not be reflected in `$rawQuery`.
3. Modifying the Query Before Execution:
If you need to make modifications to the raw query before executing it, you can use Laravel's `getRawSql()` function from the query builder or modify the generated SQL string directly with native PHP functions:
```php
$queryBuilder = DB::table('users');
$customQuery = "SELECT * FROM users WHERE id > 10"; // create a custom query
$rawQuery = $queryBuilder->getRawSql(); // get the current raw query from the builder
// modify the generated SQL string with PHP functions
$modifiedQuery = str_replace($rawQuery, $customQuery, $modifiedQuery);
echo $modifiedQuery; // prints the modified query
```
In this example, we first create a custom query and then get the current raw SQL from Laravel's query builder. We replace the original generated SQL with our custom query and print the final result. This allows us to have greater control over the execution of the query while keeping the raw query visible for debugging purposes.
4. Conclusion:
You can easily retrieve the raw query string from Laravel's query builder using the `toSql()` function if you don't need any modifications. If you need to modify the generated SQL, combine it with the `getRawSql()` or by directly modifying the returned SQL string. This flexibility ensures that developers can work with complex queries efficiently while maintaining control over the underlying database operations.
Remember to always maintain good coding practices and document your changes for better understanding of the codebase. If you would like to learn more about Laravel's query builder or any other related topics, visit our website https://laravelcompany.com/blog for helpful tutorials and guides.