How to get the query builder to output its raw SQL query as a string with PHP?

Spread the love

Sometimes, we want to get the query builder to output its raw SQL query as a string with PHP.

In this article, we’ll look at how to get the query builder to output its raw SQL query as a string with PHP.

How to get the query builder to output its raw SQL query as a string with PHP?

To get the query builder to output its raw SQL query as a string with PHP, we call the enableQueryLog and getQueryLog methods.

For instance, we write

\DB::enableQueryLog();

dd(\DB::getQueryLog());

to call \DB::enableQueryLog to enable logging.

Then we use \DB::getQueryLog() to return the result of the log.

Conclusion

To get the query builder to output its raw SQL query as a string with PHP, we call the enableQueryLog and getQueryLog methods.

Leave a Reply

Your email address will not be published.