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

Laravel Tinker Tools Laravel Package

spatie/laravel-tinker-tools

Enables using short class names in Artisan Tinker sessions on older Laravel versions (built into Laravel 5.5+). Register ShortClassNames in .psysh.php and dump optimized autoload, then reference models like NewsItem::first() without full namespaces.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation (Laravel <5.5 only):

    composer require spatie/laravel-tinker-tools
    

    Publish the config (if needed):

    php artisan vendor:publish --provider="Spatie\TinkerTools\TinkerToolsServiceProvider"
    
  2. First Use Case: Launch Tinker:

    php artisan tinker
    

    Now use short class names directly:

    User::first();  // Instead of \App\Models\User::first()
    

Key Files to Review

  • config/tinker-tools.php (if published) – Customize allowed namespaces.
  • app/Providers/AppServiceProvider.php – Check for existing Tinker aliases.

Implementation Patterns

Core Workflow

  1. Short Class Resolution: The package automatically resolves short class names (e.g., User) to fully qualified names (e.g., \App\Models\User) when:

    • The class exists in app/ or database/ directories.
    • The class is autoloaded by Composer.
  2. Integration with Existing Code:

    • Models: Use User::find(1) instead of \App\Models\User::find(1).
    • Services: Instantiate classes like MyService::newInstance().
    • Collections: Chain methods like User::where(...)->get()->pluck('name').
  3. Custom Namespaces: Extend the allowed namespaces in config/tinker-tools.php:

    'namespaces' => [
        'App\\',
        'Database\\',
        'Vendor\\Custom\\',
    ],
    
  4. Aliasing Classes: Override default behavior by defining aliases in AppServiceProvider:

    use Spatie\TinkerTools\TinkerTools;
    
    public function boot()
    {
        TinkerTools::addAlias('User', \App\Models\Admin::class);
    }
    

Gotchas and Tips

Pitfalls

  1. Laravel 5.5+: The package is deprecated for Laravel 5.5+. Use Laravel’s built-in Tinker aliases instead:

    use App\Models\User;  // No package needed
    
  2. Namespace Conflicts: If two classes share the same short name (e.g., User in App and Vendor), the first matching namespace in config/tinker-tools.php wins.

  3. Dynamic Classes: Classes loaded dynamically (e.g., via class_alias()) may not resolve. Ensure they’re autoloaded.

  4. Case Sensitivity: Short names are case-sensitive. user::first() will fail unless the class is named exactly User.

Debugging Tips

  • Verify Resolution: Check if a class resolves by running:

    \Spatie\TinkerTools\TinkerTools::resolveShortClassName('User');
    

    Returns null if unresolved.

  • Clear Cache: After modifying config/tinker-tools.php, run:

    php artisan config:clear
    
  • Log Short Names: Enable debug mode in config/tinker-tools.php:

    'debug' => true,
    

    Logs unresolved short names to storage/logs/laravel.log.

Extension Points

  1. Custom Resolvers: Extend the resolver logic by binding a custom resolver to the container:

    $this->app->bind(\Spatie\TinkerTools\ShortClassNameResolver::class, function () {
        return new CustomShortClassNameResolver();
    });
    
  2. Tinker Commands: Use the package’s TinkerTools facade in custom Tinker commands:

    use Spatie\TinkerTools\TinkerTools;
    
    TinkerTools::addAlias('DB', \Illuminate\Support\Facades\DB::class);
    
  3. IDE Support: Add @mixin in PHPStorm for short names:

    // In a Tinker helper file (e.g., `app/Tinker.php`)
    @mixin \App\Models\User
    
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport