Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Ladybug Laravel Package

raulfraile/ladybug

Ladybug is an extensible var_dump/print_r replacement for PHP 5.3+ that renders any variable, object, or resource in a clean, readable format. Simple global helper (ladybug_dump) with solid docs and framework integrations.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require raulfraile/ladybug
    

    Add to composer.json if not using Composer:

    "require": {
        "raulfraile/ladybug": "^1.0"
    }
    
  2. Basic Usage:

    use Ladybug\Dumper;
    
    $data = ['user' => ['name' => 'John', 'active' => true]];
    Dumper::dump($data); // Outputs formatted dump to stdout
    
  3. First Use Case: Replace var_dump() or dd() for debugging complex objects/arrays in Laravel:

    // Instead of:
    dd($this->user->toArray());
    
    // Use:
    Dumper::dump($this->user->toArray());
    

Key Files to Explore

  • vendor/raulfraile/ladybug/src/Ladybug/Dumper.php (Core class)
  • vendor/raulfraile/ladybug/src/Ladybug/Renderer.php (Output formatting)
  • vendor/raulfraile/ladybug/tests/ (Usage examples)

Implementation Patterns

Debugging Workflows

  1. Replacing dd():

    // In controllers/services:
    Dumper::dump($this->fetchUserData(), true); // `true` exits after dump
    
  2. Logging Debug Data:

    use Illuminate\Support\Facades\Log;
    
    Log::debug(Dumper::dump($this->query->getBindings()));
    
  3. Custom Dump Middleware (for API responses):

    // app/Http/Middleware/DebugDumper.php
    public function handle($request, Closure $next) {
        if ($request->has('debug')) {
            Dumper::dump($request->all());
            exit;
        }
        return $next($request);
    }
    

Integration Tips

  • Laravel Service Provider:

    // app/Providers/AppServiceProvider.php
    public function boot() {
        if (app()->environment('local')) {
            Dumper::setRenderer(new \Ladybug\Renderer\HtmlRenderer());
        }
    }
    
  • Blade Debugging:

    @php
        Dumper::dump($loop->items);
    @endphp
    
  • Artisan Commands:

    // app/Console/Commands/DebugCommand.php
    public function handle() {
        $data = Cache::get('debug:data');
        Dumper::dump($data);
    }
    

Extending Functionality

  1. Custom Renderers:

    class JsonRenderer extends \Ladybug\Renderer\Renderer {
        public function render($data) {
            return json_encode($data, JSON_PRETTY_PRINT);
        }
    }
    Dumper::setRenderer(new JsonRenderer());
    
  2. Hooking into Laravel Events:

    Event::listen('eloquent.updated:*', function ($model) {
        if (app()->environment('local')) {
            Dumper::dump($model->getChanges());
        }
    });
    

Gotchas and Tips

Common Pitfalls

  1. Performance Impact:

    • Avoid using in production or high-traffic environments.
    • Fix: Wrap in if (app()->environment('local')).
  2. Recursive Data Issues:

    • Circular references may cause infinite loops.
    • Fix: Use Dumper::dump($data, 5) to limit depth.
  3. Output Buffering:

    • Dumps may not appear if output is buffered (e.g., in CLI with --no-ansi).
    • Fix: Use ob_start() or force raw output:
      Dumper::dump($data, false, true); // 3rd param: force raw
      

Debugging Tips

  1. Inspecting Requests:

    Dumper::dump([
        'headers' => $request->header(),
        'input'   => $request->all(),
        'files'   => $request->file()->all(),
    ]);
    
  2. Database Queries:

    $query = User::query()->where('active', true);
    Dumper::dump($query->toSql(), $query->getBindings());
    
  3. View Debugging:

    // In a view:
    @php
        Dumper::dump($this->getDebugData());
    @endphp
    

Configuration Quirks

  1. Renderer Overrides:

    • Default renderer is Symfony\VarDumper\Cloner\VarCloner.
    • Tip: Extend Ladybug\Renderer\Renderer for custom formats.
  2. Max Depth:

    • Default depth is 10. Adjust via:
      Dumper::setMaxDepth(15);
      
  3. Handling Exceptions:

    • Use try-catch with Dumper to avoid crashes:
      try {
          $result = $this->riskyOperation();
          Dumper::dump($result);
      } catch (\Exception $e) {
          Dumper::dump($e->getTrace());
          throw $e;
      }
      

Extension Points

  1. Custom Handlers:

    Dumper::addHandler('App\Models\User', function ($user) {
        return [
            'id' => $user->id,
            'name' => $user->name,
            'email' => $user->email,
        ];
    });
    
  2. Filtering Sensitive Data:

    Dumper::setFilter(function ($data) {
        if (is_array($data) && isset($data['password'])) {
            $data['password'] = '[FILTERED]';
        }
        return $data;
    });
    
  3. Integration with Laravel Debugbar:

    // In a service provider:
    $debugbar = app(\Barryvdh\Debugbar\Debugbar::class);
    $debugbar->addCollector(new class extends \Barryvdh\Debugbar\Collector\Collector {
        public function collect() {
            return [
                'ladybug' => Dumper::dump($this->getData()),
            ];
        }
    });
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui