• Reading time ~ 1 min
  • 31.01.2024

While the Dumpable trait is technically new in Laravel 11, these I would say that it consolidates the idea of dump() and dd() into a reusable trait so that classes don’t have to manually implement this separately. Application developers and package authors can also benefit from this new illumiate/support trait to easily add debugging methods in chainable classes.

In Laravel 10, we already enjoy being able to chain in a ->dd() or ->dump() into various framework classes, such as Carbon, Stringable, the query Builder, and TestResponse:

use Illuminate\Support\Carbon;

$time = Carbon::now()
  ->addDay()
  ->addMinute()
  ->addSecond();

// dumping before adding a minute and second
$time = Carbon::now()
  ->addDay()
  ->dump()
  ->addMinute()
  ->dump()
  ->addSecond();

However, in Laravel 10, the dump() and dd() methods were created ad-hoc in each class separately, whereas, in Laravel 11, all that code was consolidated into the Dumpable trait we can use in any class. Here's an example from the Illuminate Carbon class in Laravel 11:

use Illuminate\Support\Traits\Dumpable;

class Carbon extends BaseCarbon
{
    use Conditionable, Dumpable;

    // ...
}

The implementation of dump() looks like the following in the Dumpable trait. The only difference with dd() is that the code will stop execution and exit:

public function dump(...$args)
{
    dump($this, ...$args);

    return $this;
}

Shout-out to Nuno Maduro for implementing this feature in Pull Request #47122 to clean up internal usage of these methods, but also paved the way for apps and packages to utilize this trait.

Comments

No comments yet
Yurij Finiv

Yurij Finiv

Full stack

ABOUT

Professional Fullstack Developer with extensive experience in website and desktop application development. Proficient in a wide range of tools and technologies, including Bootstrap, Tailwind, HTML5, CSS3, PUG, JavaScript, Alpine.js, jQuery, PHP, MODX, and Node.js. Skilled in website development using Symfony, MODX, and Laravel. Experience: Contributed to the development and translation of MODX3 i...

About author CrazyBoy49z
WORK EXPERIENCE
Contact
Ukraine, Lutsk
+380979856297