When you’re creating a simple project using Laravel framework, you might not need to organize your models in a specific directory. But, if you already have a lot of models to work on, it is recommended to put it in a specific folder.
By default, after creating a model through a terminal, all models go directly inside the
app folder. Our objective in this article is to know how to transfer all models in a specific directory.
Upon installation of fresh laravel framework, we already have User.php model inside of app folder. Now, when transferring of all models into a folder, consider the steps below:
1. Create a Models folder inside app. So, you have now app\Models
2. Put the User.php inside app\Models folder. The structure will look like this:
3. Change namespace of User.php.
namespace App\Models; // change to this
class User extends Authenticatable
4. When you have controllers that use the Models, you also need to change the namespace. Example, from LoginController,
Use App\Models\User; // change to this
class LoginController extends Controller
5. Open config/auth.php and change the providers setting
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class, // change this
6. Let’s say, we want to make People model. From the terminal, execute the command below:
$ php artisan make:model Models/People
If you have any comment, please feel free to add to the comment section below.