How to Get the Raw SQL Query from Laravel Query Builder
Ankit Verma

Ankit Verma @ankitvermaonline

About: Full Stack Developer 7+ years of experience in Web development, specializing in PHP frameworks.

Location:
Lucknow
Joined:
Sep 18, 2023

How to Get the Raw SQL Query from Laravel Query Builder

Publish Date: May 28
1 0

When debugging or optimizing database queries in Laravel, it's often useful to view the raw SQL query being generated. Thankfully, Laravel makes this easy with the toSql() method.

You can call toSql() on any Query Builder instance to see the SQL statement it will execute—without actually running the query.

DB::table('users')->toSql();
// Output: select * from `users`
Enter fullscreen mode Exit fullscreen mode

It also works with conditions:

User::where('status', 1)->toSql();
// Output: select * from `users` where `status` = ?
Enter fullscreen mode Exit fullscreen mode

Comments 0 total

    Add comment