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

Tinker Laravel Package

laravel/tinker

Laravel Tinker is a powerful REPL for Laravel, letting you interact with your application from the command line. Run Artisan’s tinker command to test code, inspect models, and debug quickly in an interactive shell.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require laravel/tinker
    

    Tinker is included by default in Laravel since version 5.5, so no additional installation is required in most cases.

  2. Accessing Tinker: Launch Tinker via Artisan:

    php artisan tinker
    

    Or directly from any Artisan command:

    php artisan tinker --execute="User::first()"
    
  3. First Use Case: Debug a model query or test logic interactively:

    >>> $user = User::find(1);
    => App\Models\User {#3245
        id: 1,
        name: "John Doe",
        email: "john@example.com",
      }
    >>> $user->posts()->count();
    => 5
    

Implementation Patterns

Core Workflows

  1. Debugging Queries: Use Tinker to inspect Eloquent queries without writing test cases:

    >>> $query = User::where('active', true)->toSql();
    => "select * from `users` where `active` = ?"
    >>> $query
    => Illuminate\Database\Query\Builder {#3245 ...}
    
  2. Testing Logic: Validate business logic before committing:

    >>> $order = new Order(['total' => 100]);
    >>> $order->applyDiscount(20);
    => 80
    
  3. Interactive Console Commands: Execute commands programmatically:

    >>> Artisan::call('migrate:status');
    => "Migration status output..."
    
  4. Custom Casting: Extend Tinker’s output formatting for custom objects:

    // In AppServiceProvider@boot()
    Psy\Configuration::getOption('casting')->addCaster(MyModel::class, function ($model) {
        return "Custom: {$model->id}";
    });
    

Integration Tips

  • Autoloading: Tinker automatically resolves Laravel’s service container, so all bound services (e.g., Cache, Mail) are available.
  • Environment Context: Use --env=testing to test logic in a specific environment:
    php artisan tinker --env=testing
    
  • Batch Execution: Run one-liners via --execute:
    php artisan tinker --execute="User::all()->count()"
    

Gotchas and Tips

Pitfalls

  1. Environment Mismatch: Tinker runs in the same environment as the Artisan command. Ensure .env variables are loaded correctly:

    php artisan tinker --env=production  # Use cautiously!
    
  2. PsySH Prompts: Avoid accidental trust prompts by using --no-interaction:

    php artisan tinker --no-interaction
    
  3. Memory Leaks: Complex queries or loops in Tinker can consume memory. Reset the session with:

    >>> Psy\Configuration::getOption('history')->clear();
    

Debugging Tips

  • Inspect Variables: Use var_dump() or dd() (via dump() helper) for deep inspection:

    >>> dump($user->toArray());
    
  • Autocomplete: Press Tab to autocomplete classes, methods, or properties (PsySH feature).

  • Exit Gracefully: Use exit or Ctrl+D to quit Tinker without errors.

Extension Points

  1. Custom Casters: Override default output for models or collections:

    Psy\Configuration::getOption('casting')->addCaster(Collection::class, function ($collection) {
        return "Collection[{$collection->count()}]";
    });
    
  2. PsySH Configuration: Modify Tinker’s behavior via config/tinker.php (if present) or directly in AppServiceProvider:

    Psy\Configuration::getOption('history')->setFile('/dev/null'); // Disable history
    
  3. Whitelisted Commands: Restrict Tinker to safe commands by extending TinkerCommand:

    protected function getWhitelistedCommands()
    {
        return ['migrate:status', 'cache:clear'];
    }
    

Pro Tips

  • Alias Artisan: Add a shell alias for quick access:
    alias tinker='php artisan tinker'
    
  • IDE Integration: Use PHPStorm’s "Run Tinker" shortcut (Ctrl+Shift+T) for seamless debugging.
  • Testing: Mock dependencies in Tinker for ad-hoc testing:
    >>> $mock = Mockery::mock('alias:Mailer');
    >>> $mock->shouldReceive('send')->once();
    
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.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai