Move Laravel 5 Eloquent models into its own directory

后端 未结 5 1256
你的背包
你的背包 2021-01-30 08:44

Laravel 5 has ORM models by default in app folder. I want to move these into app/models. When I do that then these classes are not found anymore.

How to make Laravel fin

相关标签:
5条回答
  • 2021-01-30 09:22

    Solution for Laravel 5.6+


    1. Move the files to the new directory

    Say you want to move the models to app/Models

    2. Change the namespace of the models

    For each model change :

    namespace App;
    

    to

    namespace App\Models;
    

    3. Change the references in other files

    Check these files and search especially app\User

    • app/Http/Controllers/Auth/RegisterController.php
    • config/auth.php
    • config/services.php
    • database/factories/ModelFactory.php
    • database/factories/UserFactory.php
    • Your Controllers

    And change App/ModelExample to App/Models/ModelExample

    4. Autoload files

    Run composer dump-autoload

    5. Congratulations!

    0 讨论(0)
  • 2021-01-30 09:25

    Add "app/models" to composer.json's classmap autoload section

    "autoload": {
        "classmap": [
            "database",
            "app/models"
        ]
    }
    
    0 讨论(0)
  • 2021-01-30 09:30

    Afer moving all files in the models folder. What you need to do is run:

    composer dump-autoload

    This worked for me. :)

    0 讨论(0)
  • 2021-01-30 09:33

    Just create a folder named Models and move your files into it. After that, change the namespace of the models from just App to App\Models (update the usages as well) and run composer dump-autoload.

    0 讨论(0)
  • 2021-01-30 09:36

    After doing @Saint Play steps. I had to do php artisan config:clear. Then it worked !

    0 讨论(0)
提交回复
热议问题