How to move the User model inside app\Models folder in Laravel

0
3065
Move the User Model - Laravel

How to move the User model inside the Models folder in laravel? This is the question every newbie keeps asking during their starting day with Laravel. Although this is not a difficult question, still many of us got scared to change them by ourselves without any guidance. So, to solve this issue and organize Laravel models we are writing this article in detail on how to move the User model (created during laravel-auth command) inside the app\Models folder.

After successfully installing laravel on windows, and creating laravel auth scaffolding you might be willing to organize the User models created by the laravel-auth command. In this article, we will show you in which files you need to update the reference of the User model, so that you could move the user model inside the app\Models folder safely. Ok then, without any delay let’s jump to the step by step guide of how to move the user model inside the app\models folder.

How to move the user model inside the app\Models folder in Laravel?

#1.  Move the user model inside the app\Models folder

Before anything, let’s create a folder named “Models” inside the app folder. Then, move the “User” model inside that newly created Models directory. As soon as you move the User model inside the app\Models folder, update the namespace of the User Model from “namespace App;” to “namespace App\Models;“.

#2. Update User Model path on Auth Controllers

Next, you need to update the path of the User model in the Register Controller. Or, in case if you felt doubt where it could be referenced inside auth controllers (RegisterController, LoginController, VerificationController, ForgotPasswordController, ResetPasswordController, and ConfirmPasswordController), then open the project folder in one of the code editors like sublime text, PHP storm, visual studio code editors, atom or any other of your choice. Then, search `User::` inside the Auth folder.

Inside RegisterController, update “use App\User;” by “use App\Models\User;“. Here, we are just updating the path of the imported User Model.

Organize Laravel Models

#3 Update config/auth.php

After updating the user model path on Auth Controller, you need to update the path on config/auth.php. There under “providers” update the user model from “App\User” to “App\Models\User“.

After updating the auth file, you should be able to Register and log in successfully for the first time after moving the user model inside the app\Models folder.

#4. Update UserFactory

Even without updating this one, you should be able to register and login users from register and login form respectively. But, if you are planning to generate some fake users to populate your database using UserFactory. Then, you must update the path of the User Model from “App\User” to “App\Models\User” in “database/factories/UserFactory“.

#5. Run composer dump-autoload

Lastly, you need to run the composer dump-autoload. It won’t download anything new but looks for all the classes that need to be included again. It just regenerates the list of all classes that need to be included in the project. To check these autoloaded classes, you need to open composer.json and look under the autoload section.

Now, you have already moved your User Model inside app/Models folder, you are all set to get started. From now on, whenever you want to create a new model, run php artisan make:model Models/ModelName instead of php artisan make:model ModelName. With this approach, all your models with then are placed inside the app\Models folder.

This much for today, if you find any confusion in any steps, please don’t hesitate to raise your issue on the comment section.

 

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here