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

Kint Laravel Package

kint-php/kint

Kint is a powerful PHP debugging and profiling tool that dumps variables with rich, readable output (CLI and browser). It offers deep inspection of arrays/objects, stack traces, timing/memory info, and easy integration for faster troubleshooting in any PHP project.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require kint-php/kint
    

    No additional configuration is required—Kint auto-detects Laravel and integrates seamlessly.

  2. First Use: Replace var_dump() or dd() with:

    kint($yourVariable); // For inline inspection
    

    Or use the Laravel-friendly dd() alias (if enabled):

    dd($yourVariable); // Dumps and dies (same as Laravel's dd)
    
  3. Where to Look First:

    • Browser Output: Inspect variables in a collapsible, interactive panel.
    • CLI Output: Colorized, structured dumps with syntax highlighting.
    • Documentation: Official Kint Docs (focus on the "Usage" section).

First Use Case: Debugging a Query Result

$user = User::with('posts')->find(1);
kint($user); // Inspect the entire Eloquent relationship tree
  • Why? Kint reveals nested relationships, lazy collections, and query metadata (e.g., SQL executed) in a readable format.
  • Pro Tip: Use kint($user->toArray()) to flatten the output for simpler inspection.

Implementation Patterns

Core Workflows

1. Replacing var_dump() and dd()

  • Inline Debugging:
    kint($request->all()); // Inspect incoming request data
    
  • Terminating Execution:
    dd($this->someComplexObject); // Kint + die (Laravel-compatible)
    

2. CLI Debugging

  • Artisan Commands:
    kint($this->getSomeData()); // Colorized CLI output
    
  • Queue Workers:
    kint($job->payload()); // Debug failed jobs
    

3. Profiling and Performance

  • Memory/Time Tracking:
    kint(memory_get_usage(), memory_get_peak_usage());
    
  • Benchmarking:
    $start = microtime(true);
    // ... code ...
    kint(microtime(true) - $start);
    

4. Laravel-Specific Patterns

  • Middleware Debugging:
    kint($request->headers->all()); // Inspect incoming headers
    
  • Service Container Inspection:
    kint(app()->bound('some.bound.service')); // Check if a service is bound
    
  • Event Listeners:
    kint($event->data); // Inspect event payloads
    

5. Custom Dumpers for Eloquent

  • Override default behavior for models:
    use Kint\Kint;
    
    Kint::registerDumper(User::class, function ($user) {
        return [
            'id' => $user->id,
            'name' => $user->name,
            'posts_count' => $user->posts()->count(),
        ];
    });
    

Integration Tips

Laravel Service Provider

Add Kint to Laravel’s AppServiceProvider for global access:

public function boot()
{
    if ($this->app->environment('local')) {
        \Kint::register();
    }
}

IDE Integration

  • PHPStorm: Use Kint’s output as a "Debugger" alternative by setting breakpoints and inspecting variables via Kint.
  • VSCode: Pair with the "PHP Intelephense" extension for variable hover previews + Kint for deep inspection.

API Debugging

  • JSON Responses:
    kint(json_decode($response->getContent(), true));
    
  • Request/Response Logging:
    kint([
        'request' => $request->all(),
        'response' => $response->getData(),
    ]);
    

Gotchas and Tips

Pitfalls

  1. Performance Overhead:

    • Issue: Kint can slow down production-like environments due to deep introspection.
    • Fix: Disable in non-local environments:
      if (app()->environment('local')) {
          kint($data);
      }
      
  2. Recursive Data:

    • Issue: Infinite loops when dumping circular references (e.g., User <-> Role many-to-many).
    • Fix: Set a depth limit:
      kint($user, ['maxDepth' => 3]);
      
      Or configure globally in config/kint.php:
      'maxDepth' => 5,
      
  3. CLI vs. Web Rendering:

    • Issue: CLI output may not render as expected in web contexts (and vice versa).
    • Fix: Force a renderer:
      kint($data, ['renderer' => \Kint\Renderer\WebRenderer::class]);
      
  4. Laravel Debugbar Conflict:

    • Issue: Kint and barryvdh/laravel-debugbar may clash.
    • Fix: Disable Debugbar’s dumper or use Kint exclusively.
  5. Sensitive Data Exposure:

    • Issue: Accidentally dumping passwords, tokens, or API keys.
    • Fix: Sanitize data before dumping:
      $sanitized = collect($request->all())->except(['password', 'api_token']);
      kint($sanitized);
      

Debugging Tips

  1. Inspecting Closures/Lambdas:

    • Kint can dump closures, but output may be cryptic. Use:
      kint($closure->getClosureThis()); // Inspect bound object
      
  2. Database Query Debugging:

    • Dump the query builder instance to see raw SQL:
      $query = User::where('active', true);
      kint($query->toSql(), [$query->getBindings()]);
      
  3. Symfony Components:

    • Kint works with Symfony’s ParameterBag, ArrayAccess, etc.:
      kint($request->query); // Symfony's ParameterBag
      
  4. Custom Objects:

    • Implement __debugInfo() for cleaner output:
      class MyModel {
          public function __debugInfo() {
              return [
                  'id' => $this->id,
                  'name' => $this->name,
              ];
          }
      }
      

Configuration Quirks

  1. Global Configuration:

    • Override defaults in config/kint.php:
      'enabled' => env('KINT_ENABLED', true),
      'maxDepth' => 10,
      'exclude' => [
          'password',
          'api_token',
          'remember_token',
      ],
      
  2. Renderer Switching:

    • Use Kint::setRenderer() dynamically:
      \Kint::setRenderer(\Kint\Renderer\CliRenderer::class); // Force CLI
      
  3. Plugin System:

    • Extend Kint with custom plugins (e.g., for Laravel-specific data):
      \Kint::registerPlugin(new class {
          public function getName() { return 'Laravel'; }
          public function dump($data) {
              if ($data instanceof \Illuminate\Database\Eloquent\Model) {
                  return $data->toArray();
              }
          }
      });
      

Extension Points

  1. Custom Dumper for Collections:

    use Illuminate\Support\Collection;
    use Kint\Kint;
    
    Kint::registerDumper(Collection::class, function ($collection) {
        return [
            'count' => $collection->count(),
            'first' => $collection->first(),
            'last' => $collection->last(),
            'keys' => $collection->keys()->toArray(),
        ];
    });
    
  2. Hook into Laravel Events:

    • Dump data during specific events (e.g., Illuminate\Auth\Events\Registered):
      event(new Registered($user));
      kint($user); // Inspect the newly created user
      
  3. TAP Testing:

    • Use Kint in PHPUnit tests for interactive debugging:
      public function testSomething()
      {
          $result = $this->someMethod();
          kint($result); // Inspect during test runs
          $this->assertTrue($result);
      }
      
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.
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor
spatie/laravel-javascript-views