Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jfromaniello's avatar

make tinker to autoload model classes

I've a project with an strange issue. For the same configuration if I run tinker on heroku like this, it will automatically load my classes without writing the use clause:

$ heroku run php artisan tinker
Psy Shell v0.11.12 (PHP 7.4.33 — cli) by Justin Hileman
>>> User::first()
[!] Aliasing 'User' to 'App\Models\User' for this Tinker session.

but if I do this locally on my dev machine, it doesn't work:

» php artisan tinker
Psy Shell v0.11.12 (PHP 7.4.33 — cli) by Justin Hileman
> User::first()

   Error  Class 'User' not found.

> use App\Model\User
> User::first()
= App\User {#5027
....

As you can see, is the same exact version in both places. I couldn't find much documentation on what is needed to make this work.

I am new to laravel, I appreciate any help.

Thanks in advance.

0 likes
2 replies
kiwi0134's avatar

Hello,

Tinker can actually be configured, though the config file doesn't exist by default. You can publish it with:

php artisan vendor:publish --provider="Laravel\Tinker\TinkerServiceProvider"

Now, inside your config/tinker.php, you'll find a key named alias, which can be used to alias certain classes by default.

Though files inside the app/ directory should be aliased automatically. But I don't know why it's ont the cae for you. You might want to try to refresh your composer autoloader with composer dump-autoload. Don't know if this helps.

2 likes
so_yi_jung's avatar

Before entering php tinker, run composer dump-autoload

5 likes

Please or to participate in this conversation.