Your destination for complete Tech news

How to get the raw SQL query from the query builder in Laravel

262 0
< 1 min read

To get the SQL query from the query builder in Laravel as string, you can use the toSql() method on the query builder instance.

For Example: you want to get the raw SQL query for the following.

DB::table('users')->toSql();
// This will return 'select * from users;'

DB::table('users')->where('role', 1)->toSql();
// select * from users where role = ?
// Note that the above returned only the raw parameters. 
// To get the value bindings you need to use the method getBindings();

DB:table('users')->where('role', 1)->getBindings();
// This will return `[ 1 ]`

Leave A Reply

Your email address will not be published.

four × two =

This site uses Akismet to reduce spam. Learn how your comment data is processed.