symfony/var-dumper
Symfony VarDumper provides a powerful dump() replacement for var_dump(), letting you inspect complex PHP variables with rich, readable output. Includes advanced casters, configurable formatters, and tooling for debugging in CLI and web contexts.
Installation:
composer require symfony/var-dumper
Laravel already includes this package via Symfony’s dependencies, so no additional installation is required in most cases.
First Use Case:
Replace var_dump() or dd() with Symfony’s dump():
use Symfony\Component\VarDumper\VarDumper;
// Basic usage
$user = User::find(1);
VarDumper::dump($user);
// Shortcut (Laravel already provides this via helpers)
dump($user); // Alias for VarDumper::dump()
dd($user); // Alias for dump() + die()
Where to Look First:
dump() and dd() helpers (no extra config needed).q to quit).// Debug a user with nested relationships
$user = User::with('posts.comments')->find(1);
dump($user); // Shows relationships in a collapsible tree
// Debug a specific relationship
dump($user->posts->where('published', true)->load('author'));
// Debug API responses (works with Laravel HTTP clients)
$response = Http::get('https://api.example.com/data');
dump($response->json()); // Pretty-printed JSON with syntax highlighting
// Debug Symfony HTTP responses (if using Symfony components)
$response = $client->request('GET', '/endpoint');
dump($response->getContent());
// Debug queue payloads (e.g., in a job class)
public function handle()
{
dump('Job payload:', $this->data); // Inspect job data
dump('Exception:', $this->exception); // If job fails
}
// Debug Artisan command input/output
public function handle()
{
dump('Input:', $this->argument('input'));
dump('Options:', $this->option('verbose'));
}
public function handle(OrderPlaced $event)
{
dump('Event payload:', $event->order); // Inspect event data
}
Extend VarDumper to handle custom objects (e.g., Eloquent models with complex attributes):
use Symfony\Component\VarDumper\Caster\Caster;
class EloquentModelCaster implements Caster
{
public function __invoke($model)
{
return [
'class' => get_class($model),
'attributes' => $model->attributesToArray(),
'relations' => $model->relations,
];
}
public function getType()
{
return 'eloquent_model';
}
}
// Register the caster (e.g., in a service provider)
VarDumper::addCaster(new EloquentModelCaster());
Use dump() in Blade to inspect variables during template rendering:
@dump($user) {{-- Renders HTML output in browser --}}
@dd($user) {{-- Renders and stops execution --}}
Use Laravel’s debug mode to toggle VarDumper output:
if (app()->environment('local')) {
dump('Debug data:', $variable);
}
Catch exceptions and dump them for debugging:
try {
// Risky operation
} catch (\Exception $e) {
dump('Exception:', $e);
throw $e;
}
Avoid dumping large datasets by limiting results:
$largeCollection = User::all();
dump($largeCollection->take(10)); // Only dump first 10 items
If using barryvdh/laravel-debugbar, VarDumper’s output can be redirected to the debug bar:
use Barryvdh\Debugbar\Debugbar;
Debugbar::info(VarDumper::dump($variable));
Memory Usage:
->take(N) or sample data before dumping.Sensitive Data Exposure:
dump() calls in production can leak sensitive data (passwords, tokens).if (app()->environment('local')) or use feature flags.Performance Overhead:
APP_DEBUG=false) or use structured logging.CLI vs. Web Output Mismatch:
dump() in CLI and @dump() in Blade for consistent debugging.Custom Object Dumping:
Deprecated Methods:
__sleep()).__serialize()/__unserialize().Inspecting Request/Response Cycles: Use middleware to dump requests/responses:
public function handle($request, Closure $next)
{
dump('Request:', $request->all());
$response = $next($request);
dump('Response:', $response->getContent());
return $response;
}
Debugging Laravel Services: Dump service container bindings:
dump(app()->bindings()); // List all service bindings
dump(app('db')); // Inspect database connection
Debugging Blade Directives: Create a custom Blade directive for debugging:
Blade::directive('debug', function ($expression) {
return "<?php dump({$expression}); ?>";
});
Usage:
@debug($user)
Debugging Queue Failures: Use a job middleware to dump failed jobs:
public function handle($job, $next)
{
try {
return $next($job);
} catch (\Exception $e) {
dump('Failed job:', $job->payload());
throw $e;
}
}
Debugging Symfony Events: Dump event dispatchers or listeners:
dump($dispatcher->getListeners('event.name')); // List listeners
Custom Casters:
Output Formatters:
ClonerInterface or DumperInterface to customize output (e.g., JSON, XML, or custom formats).Integration with Laravel:
dump()/dd() helpers in app/Helpers.php or a service provider:
if (!function_exists('custom_dump')) {
function custom_dump(...$args) {
// Custom logic (e.g., log to a file, filter sensitive data)
VarDumper::dump(...$args);
}
}
CLI Enhancements:
Cloner to serialize data for CLI tools:
use Symfony\Component\VarDumper\Cloner\Cloner;
$cloner = new Cloner();
$serialized = $cloner
How can I help you explore Laravel packages today?