donatj/drop
Lightweight PHP debug helper with drop() and see() for quickly printing one or more variables in a readable format on web or CLI. drop() dumps values and exits (status 1); see() dumps without halting. Ideal for quick “print statement” debugging.
Installation:
composer require donatj/drop
Add to composer.json if needed (e.g., for dev-only dependencies):
"require-dev": {
"donatj/drop": "^1.1.1"
}
First Use Case:
Replace var_dump() or dd() in quick debugging scenarios:
use function drop;
$user = User::find(1);
drop($user->toArray(), $user->roles); // Halts execution
Where to Look First:
drop(): For halting execution and inspecting variables (like dd()).see(): For non-blocking inspection (like dump()).Replacing dd()/var_dump():
// Before:
dd($request->all());
// After:
drop($request->all()); // Halts execution, cleaner output
Non-Blocking Debugging:
see($this->model->fresh()->load('relations')); // Logs to output, continues execution
CLI-Specific Debugging:
php artisan tinker
>>> drop(app()->make('someService')->getData());
dd() would break the flow.Integration with Laravel Facades:
drop(
Auth::user(),
Cache::get('key'),
Route::current()->parameters()
);
Conditional Debugging:
if (app()->environment('local')) {
see('Debug data:', $someVariable);
}
Logging to Files: Redirect output to a file in CLI:
php script.php 2> debug.log
Then use see() to log critical data.
Custom Formatting: Extend the package by overriding the output logic (see Gotchas for hooks).
Testing:
Mock drop()/see() in tests to avoid halting execution:
$this->expectOutputString('Expected output');
drop('test'); // Won't halt in tests if output is captured
Execution Halt:
drop() always exits with status 1. Avoid in production or use see() instead.if (!app()->runningInConsole() && !app()->environment('local')) {
return;
}
drop($data);
Output Buffering:
drop()/see().while (ob_get_level()) {
ob_end_clean();
}
drop($data);
Complex Objects:
->toArray() or ->jsonSerialize() first:
drop($model->toArray()['specific_key']);
CLI vs. Web Formatting:
Silent Failures:
If drop()/see() doesn’t output, check for:
ob_start() calls).exit() or die() in your code.error_log('Drop called') before the call.Performance:
Avoid drop() in loops or performance-critical paths. Use see() sparingly.
Custom Output Format: Override the output logic by extending the package (not officially supported but possible):
// vendor/donatj/drop/src/functions.php
function drop(...$args) {
// Custom logic here
exit(1);
}
Integration with Monolog:
Redirect see() output to a logger:
see('Log message'); // Outputs to CLI *and* logs via Monolog
Configuration: No config file exists, but you can create a helper:
if (config('app.debug')) {
see('Debug data:', $variable);
}
Alias for Convenience:
Add to composer.json aliases:
"autoload": {
"files": ["app/helpers.php"]
}
Then in app/helpers.php:
if (!function_exists('dd')) {
function dd(...$args) {
drop(...$args);
}
}
Artisan Commands:
Use see() to log command progress:
public function handle() {
see('Starting task...');
// ...
}
IDE Support:
Add @mixin in PHPDoc to hint at the functions:
/**
* @mixin \drop(...$args)
* @mixin \see(...$args)
*/
How can I help you explore Laravel packages today?