As a programmer, we need to debug the code before the actual launch. So, it’s important to print the data to debug in Laravel for this we can use print_r, var_dump, die, or dd.
So, if you want to use dump() to print the data, you can use die() to halt the process. Otherwise, dd() can do the same.
var_dump() and print_r() will show the messy array of Laravel collection to solve this you can also convert Laravel collection into array and print with dd(), dump() or var_dump() and print_r().
Public function list(){ $data = task::all(); dd($data->toArray()); or dump($data->toArray()); die(); }
I prefer to use <pre> tag surround the php’s native function print_r() to prettify the array data.
Public function list(){ $data = task::all(); echo “<pre>”; print_r($data->toArray()); echo “</pre>”; die(); }
Public function list(){ $data = task::all(); echo “<pre>”; Var_dump ($data->toArray()); echo “</pre>”; die(); }
The difference between var_dump() and print_r() is that var_dump() function displays very detailed and structured information about the array and it’s elements.
It shows the data-types for each element’s value whereas print_r() simply displays the array in a human-readable form without any extra information about the array.
The best use of var_dump() over print_r() is in the case of null value like print_r(null) will display nothing whereas var_dump(null) will display NULL which is very-very useful in debugging.If you are working with ajax you can return an error in json form and then can show data in console.log().
Public function listData(){ $data = task::all(); Return response()->json($data); }
You can also customize the Laravel error handling https://laravel.com/docs/5.8/errors
If you like this article, feel free to comment and share this article. Your every comment and share is valuable for me.
Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…
Let's understand about how to use coding to improve your website's SEO. In today’s computerized…
Let's understand the most important linux commands for web developers. Linux, as an open-source and…
Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…
Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…
Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…
View Comments