03
FebGalahad introduced a package called Laravel Dumper that can improve the default output of dump() and dd() in Laravel projects. It can improve the default dump behavior for many-core Laravel objects, including:
You can install this package as a dev dependency via composer by running this command.
composer require glhd/laravel-dumper --dev
Just use dd()
as you would normally, and enjoy the newly curated output! If for some reason, you really need the full debug output for an object that laravel-dumper customizes, you can do a full
dump with ddf()
and dumpf()
.
You can see comparisons between the default dd()
output and the laravel-dumper output in the diffs directory of this repository.
If there are objects in your project that you would like to customize the dd()
behavior for, you can register custom casters using the CustomCaster
class:
use Glhd\LaravelDumper\Casters\CustomCaster;
CustomCaster::for(User::class)
->only(['attributes', 'exists', 'wasRecentlyCreated']) // Props to keep (or use `except` to exclude)
->virtual('admin', fn(User $user) => $user->isAdmin()) // Add virtual props
->filter() // Filter out empty/null props (accepts callback)
->reorder(['attributes', 'admin', '*']); // Adjust the order of props
The reorder
method accepts an array of patterns. For example, the default Model caster
uses the following ordering rules:
$order = [
'id',
'*_id',
'*',
'*_at',
'created_at',
'updated_at',
'deleted_at',
];
This ensures that id
is always first, followed by all foreign keys, followed by all other attributes, and then finally followed by timestamp attributes (with deleted_at
last). By applying bespoke ordering rules, you can make sure that the properties you usually need to debug are at the top of the dd()
output.
It’s also possible to register your own casters for any class by publishing the laravel-dumper
config file and registering your custom classes in the casters
section of the config. This gives you the same level of control over the dd()
output as the core Symfony VarDumper
package but is more complex to implement.
Your custom casters should extend Glhd\LaravelDumper\Casters\Caster
and implement the cast
method. See any of our built-in casters for more details.
For more details, you can visit its documentation and source code on Github.
Published at : 03-02-2022
I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.
Launch project