Search code examples
laravelnamespaceslaravel-artisanlaravel-7

Alias to call model class in artisan tinker


This question might sound silly, but I really wish to know if there is a way to solve the problem of long model path in artisan commands.

I have a models organized in the following structure:

`app\Models\Auth\User.php`

And in the User model, I define the namespace as this:

namespace App\Models\Auth;

class User extends Authenticatable
{

}

Now every time I need to access that model, for example in artisan tinker command, I need to write this:

>>> Models\Auth\User::create([..]);

If I omit the full path, I just get the error failed to open stream: No such file or directory

Is there a good way to give an alias to the model classes so that I can easily access it everywhere?

Is the config/app.php a good place for that? I never saw anyone putting models in aliases section and I'm not sure if it's a good idea.


Solution

  • I just realized that it comes out of the box, all I need to do is run the following command:

    composer dump-autoload
    

    And that will refresh the autoload class definitions.