Folder for Models Laravel 5.1

Asked

Viewed 2,455 times

2

Good morning, different from Laravel 4, the version 5.1 creates the Models directly in the folder App, wanted to know if it will influence something if I create folders to better organize these Models.

  • Well you will have to add the path is for example "folder.index";

1 answer

3


You will have to change the namespace of the model classes. For example

namespace App\Models;

If the folder you create is called Models (case sensitive) and is within app. This is because the composer.json is configured to auto-load the classes inside the app folder in the PSR-4 template.

"autoload": {
    "classmap": [
        "database"            
    ],
    "psr-4": {
        "App\\": "app/",
        "App\\Models\\": "app/Models/"
    }
}

After the change, you will have to run a composer dumpautoload to generate the new class of autoload.


If you are using Laravel’s native authentication systems, you will need to change the file auth.php in config.

/*
|--------------------------------------------------------------------------
| Authentication Model
|--------------------------------------------------------------------------
|
| When using the "Eloquent" authentication driver, we need to know which
| Eloquent model should be used to retrieve your users. Of course, it
| is often just the "User" model but you may use whatever you like.
|
*/

'model' => App\Models\User::class,

If you are using JWT, you will need to change the jwt.php in config

/*
|--------------------------------------------------------------------------
| User Model namespace
|--------------------------------------------------------------------------
|
| Specify the full namespace to your User model.
| e.g. 'Acme\Entities\User'
|
*/

'user' => 'App\Models\User',

In short

Personally, I believe it is good practice to create templates within a specific folder. Laravel offers easy reconfiguration of all dependencies for this.

  • Show!! Thank you so much for the help and tips Marco!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.