Move Laravel 5 Eloquent models into its own directory

后端 未结 5 1264
你的背包
你的背包 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!

提交回复
热议问题