将Laravel 5 Eloquent模型移动到其自己的目录中

时间:2022-10-16 08:15:25

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.

Laravel 5默认在app文件夹中有ORM模型。我想把它们移到app / models中。当我这样做时,再也找不到这些类了。

How to make Laravel find the Eloquent ORM models from app/models?

如何让Laravel从app / models中找到Eloquent ORM模型?

4 个解决方案

#1


25  

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.

只需创建一个名为Models的文件夹,然后将文件移入其中。之后,将模型的命名空间从App更改为App \ Models(也更新用法)并运行composer dump-autoload。

#2


25  

Solution for Laravel 5.6+


1. Move the files to the new directory

Say you want to move the models to app/Models

假设您要将模型移动到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 \ User

  • app/Http/Controllers/Auth/RegisterController.php
  • 应用程序/ HTTP /控制器/认证/ RegisterController.php
  • config/auth.php
  • 配置/ auth.php
  • config/services.php
  • 配置/ services.php
  • database/factories/ModelFactory.php
  • 数据库/工厂/ ModelFactory.php
  • database/factories/UserFactory.php
  • 数据库/工厂/ UserFactory.php
  • Your Controllers
  • 你的控制器

And change App/ModelExample to App/Models/ModelExample

并将App / ModelExample更改为App / Models / ModelExample

4. Autoload files

Run composer dump-autoload

运行composer dump-autoload

5. Congratulations!

#3


11  

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

将“app / models”添加到composer.json的classmap autoload部分

"autoload": {
    "classmap": [
        "database",
        "app/models"
    ]
}

#4


1  

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

移动models文件夹中的所有文件。你需要做的是运行:

composer dump-autoload

composer dump-autoload

This worked for me. :)

这对我有用。 :)

#1


25  

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.

只需创建一个名为Models的文件夹,然后将文件移入其中。之后,将模型的命名空间从App更改为App \ Models(也更新用法)并运行composer dump-autoload。

#2


25  

Solution for Laravel 5.6+


1. Move the files to the new directory

Say you want to move the models to app/Models

假设您要将模型移动到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 \ User

  • app/Http/Controllers/Auth/RegisterController.php
  • 应用程序/ HTTP /控制器/认证/ RegisterController.php
  • config/auth.php
  • 配置/ auth.php
  • config/services.php
  • 配置/ services.php
  • database/factories/ModelFactory.php
  • 数据库/工厂/ ModelFactory.php
  • database/factories/UserFactory.php
  • 数据库/工厂/ UserFactory.php
  • Your Controllers
  • 你的控制器

And change App/ModelExample to App/Models/ModelExample

并将App / ModelExample更改为App / Models / ModelExample

4. Autoload files

Run composer dump-autoload

运行composer dump-autoload

5. Congratulations!

#3


11  

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

将“app / models”添加到composer.json的classmap autoload部分

"autoload": {
    "classmap": [
        "database",
        "app/models"
    ]
}

#4


1  

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

移动models文件夹中的所有文件。你需要做的是运行:

composer dump-autoload

composer dump-autoload

This worked for me. :)

这对我有用。 :)