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.
The post Laravel 11 Introduces the Dumpable Trait appeared first on Laravel News.
Join the Laravel Newsletter to get all the latest Laravel articles like this directly in your inbox.
Read more https://laravel-news.com/laravel-11-dumpable-trait