Home » How To Print Or Debug Query In Laravel

How To Print Or Debug Query In Laravel

Last updated on May 26, 2022 by

In this article, we will learn how to print or debug queries in Laravel. Sometimes, it is required to view which query we are building. Mostly, we need to print the queries which are large in nature. Laravel provides built-in functions to do our job. So let’s see how to print or debug query In Laravel.

Print Or Debug Query In Laravel

There are several ways to print the queries. Let’s see them one by one.

01 Using dd() Method

The dd method will print the query information and then stop executing the request.

$users = User::where('id', '=', 1)->dd();

$users = DB::table('users')->where('id', '=', 1)->dd();
Debugging Query In Laravel Using dd()
Debugging Query In Laravel Using dd()

02 Using dump() Method

The dump method is similar to a dd. The dd means Dump & Die while we are using dump only here. That means it will print the query information but not stop the execution of the request. Let’s see the example.

$users = User::where('id', '=', 1)->dump();

$users = DB::table('users')->where('id', '=', 1)->dump();
Debugging Query In Laravel Using dump Method
Debugging Query In Laravel Using dump()

You can see on the above screenshot that script is still executing and dump display the query.

03 Using toSql() Method

This function is referenced in the Laravel 5.3 upgrading guide but can’t find out in the latest document. But it’s also working in the latest version of Laravel.

$users = User::where('id', '=', 1)->toSql();
dd($users);

Ultimately, toSql() fetching raw query and the with use of dd(), we are printing it. Instead, we should use the dd() method as shown above.

Additionally, read our guide:

  1. Laravel One To One Relationship Tutorial
  2. Laravel One To Many Relationship Tutorial With Example
  3. Database Records Update In Laravel
  4. Best Way to Remove Public from URL in Laravel
  5. Error After php artisan config:cache In Laravel
  6. Specified Key Was Too Long Error In Laravel
  7. AJAX PHP Post Request With Example
  8. How To Use The Laravel Soft Delete
  9. How To Add Laravel Next Prev Pagination
  10. Laravel Remove Package With Example (Correct Way)
  11. Difference Between Factory And Seeders In Laravel
  12. Laravel: Increase Quantity If Product Already Exists In Cart
  13. Laravel DataTables Tutorial With Example
  14. Laravel Send Mail From Localhost
  15. How To Convert Word To PDF In Laravel
  16. Laravel Multiple Where Conditions With Example

That’s it for now. We hope this article helped you to learn how to print or debug query in Laravel

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you in advance. 🙂 Keep Smiling! Happy Coding!

 
 

2 thoughts on “How To Print Or Debug Query In Laravel”

Leave a Comment