In this article, we will learn how to print or debug query 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 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();
02 Using dump() Method
The dump method is similar like a dd. The dd means die & dump 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();
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:
- Laravel One To One Relationship Tutorial
- Laravel One To Many Relationship Tutorial With Example
- Database Records Update In Laravel
- Best Way to Remove Public from URL in Laravel
- Error After php artisan config:cache In Laravel
- Specified Key Was Too Long Error In Laravel
- AJAX PHP Post Request With Example
- How To Use The Laravel Soft Delete
- How To Add Laravel Next Prev Pagination
- Laravel Remove Package With Example (Correct Way)
- Difference Between Factory And Seeders In Laravel
- Laravel: Increase Quantity If Product Already Exists In Cart
- Laravel DataTables Tutorial With Example
- Laravel Send Mail From Localhost
- How To Convert Word To PDF In Laravel
- 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!