Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing namespaces in Laravel Tinker REPL

Laravel's Tinker REPL is useful for conveniently experimenting with models, however it does not seem possible to import a namespace, requiring the model namespace to be laboriously typed. For instance, this works:

$ php artisan tinker
[1] $list = new mysweetapp\Todolist;
[2] > echo get_class($list);
mysweetapp\Todolist

This does not:

$ php artisan tinker
[1] use mysweetapp\Todolist;
// false
[2] $list = new Todolist;
[3] echo get_class($list);
Boris\EvalWorker

Is there some way to import namespaces into Tinker or is it just not yet supported? Mind you I definitely want to use namespaces, I just don't want to repeatedly type in the namespace. :-)

like image 435
Jason Gilmore Avatar asked Feb 01 '26 15:02

Jason Gilmore


1 Answers

Look at this https://softonsofa.com/tinker-like-a-boss-in-psysh/


You can't do this with use namespace in the repl itself, but here's what you may do:

// config/local/app.php
'aliases' => append_config([
    'Todolist'   => 'Mysweetapp\Todolist',
    ... // more
]),

Then in your local env you will be able to access your models without typing namespace, and if you're not in local env, then run tinker forcing it:

php artisan tinker --env=local
like image 85
Jarek Tkaczyk Avatar answered Feb 03 '26 05:02

Jarek Tkaczyk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!