CodeBriefly
Tech Magazine

Laravel 5.6 – Debugging with dump and dd Functions

0 18,097

Get real time updates directly on you device, subscribe now.

In this article, we will discuss how can we handle Laravel Debugging. Laravel offers two effective and useful debugging methods dd() and dump(). As we know, Laravel provides rich features to fulfill our minor to complex requirements. I’m assuming you are familiar with the Laravel framework if not then you can start with given tutorials.

Method dd() and dump()

Laravel provides two predefine methods dd() and dump() for debugging. You can call these methods on collection instances. You can read official docs for dump() and dd() methods.

In previous Laravel versions, when we debug collections then we assign a variable to a collection then keep dumping the variable as we altered the collection.

$posts = Post::where('status','published')->get();
dd($posts); // Here dd() will dump the resultset and stop the execution.

In Laravel 5.5, we can call dd() or dump() directly on a collection instance. These methods are making debugging a lot easier. For example: assume we had a collection of posts which went through a series of transformations and we wanted to inspect the collection at each step, then this will do:

$posts = Post::all();
$posts->dump()
  ->sortBy('created_at')
  ->dump()
  ->take(10)
  ->pluck('post_title')
  ->dd() // here dd will dump the resulst and stops the execuation. 
  ->take(1);

Difference between dd() and dump()

Both function used for debugging. But having one difference. dump() output the results and then continues processing. dd() output the results and stops the process immediately (dd stands for dump and die).

Hope you like this post. Please feel free to add the comment below if any query.

If you like our content, please consider buying us a coffee.
Thank you for your support!
Buy Me a Coffee

Get real time updates directly on you device, subscribe now.

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. AcceptRead More