php 如何指示工匠将模型保存到特定目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28225163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:45:32  来源:igfitidea点击:

How do I instruct artisan to save model to specific directory?

phplaravellaravel-5

提问by James Jeffery

I'm using Laravel 5. I have created a /Modelsdirectory under the /Appdirectory, but when generating the models using Artisan it's storing them under the Appdirectory.

我正在使用 Laravel 5。我在该/Models目录下创建了一个目录/App,但是在使用 Artisan 生成模型时,它将它们存储在该App目录下。

I have searched the documentation to try and find how to specify a different path name, but to no avail:

我搜索了文档以尝试找到如何指定不同的路径名,但无济于事:

php artisan make:model TestModel

How do I instruct artisanto save the model to specific directory?

如何指示artisan将模型保存到特定目录?

采纳答案by Bogdan

If you want to specify the path when generating a model, you can use the Laravel Generators Package. You can then specify the location using the --pathoption like so:

如果要在生成模型时指定路径,可以使用Laravel Generators Package。然后,您可以使用如下--path选项指定位置:

php artisan generate:model TestModel --path=my/custom/location

回答by Mansoor Akhtar

Create a Models directory or whatever your want to named it, put it in inside app directory. The Directory structure should look like

创建一个 Models 目录或任何你想命名的目录,把它放在 app 目录中。目录结构应如下所示

laravel-project
     /app
        /Console
        /Events
        /Exceptions
        /Http
        /Jobs
        /Listeners
        /Provider
       /Models

Then You just need to type artisan command for creating models inside Models directory

然后你只需要在 Models 目录中输入 artisan 命令来创建模型

php artisan make:model Models/ModelName 

After Creating Models your namespace inside model classes will be

创建模型后,模型类中的命名空间将是

namespace app-name\Models\ModelName

You can access this model in inside your controller

您可以在控制器内部访问此模型

use app-name\Models\ModelName

回答by mmieluch

For those using Laravel >= 5.2

对于那些使用 Laravel >= 5.2 的人

It is possible to generate a model in a subdirectory using built-in Artisan generators by "escaping" the backslashes in the FQN, like so:

通过“转义”FQN 中的反斜杠,可以使用内置的 Artisan 生成器在子目录中生成模型,如下所示:

Laravel 5.2

Laravel 5.2

php artisan model:make App\Models\Foo

Laravel 5.3

Laravel 5.3

php artisan make:model App\Models\Foo

(the difference between 5.2 and 5.3 pointed out by @Khaled Rahman, thanks!)

@Khaled Rahman指出 5.2 和 5.3 之间的区别,谢谢!)

Commands above would create Foo.phpfile in the app/Models directory and update the namespace accordingly.

上面的命令将Foo.php在 app/Models 目录中创建文件并相应地更新命名空间。

Hope that helps.

希望有帮助。

回答by Jazzzzzz

In Laravel 5.4 or later

在 Laravel 5.4 或更高版本中

You can create as below

您可以创建如下

 > php artisan make:model "Models\userModel"

here Modelsis directory name and userModelis model name

这里Models是目录名,userModel是模型名

Use " (double quotes) or ' (single quotes) to create model

使用 " (双引号) 或 ' (单引号) 创建模型

回答by Faruk Arslan

You can simply use this (for Laravel 5.3+):

你可以简单地使用这个(对于 Laravel 5.3+):

php artisan make:model your_path/model_name

If you don't specify a path it will use main 'app' folder as root. So you can navigate from there.

如果您不指定路径,它将使用主“app”文件夹作为根。所以你可以从那里导航。

Models/Folder1/MyModel means app->Models->Folder1->MyModel.php

Models/Folder1/MyModel 表示 app->Models->Folder1->MyModel.php

回答by kush

Controller path (ApI/Admin)

控制器路径(ApI/Admin)

Model path(Model/Admin)

模型路径(模型/管理员)

php artisan make:controller API/Admin/PlanController --model=Model/Admin/Plan --resource

php artisan make:controller API/Admin/PlanController --model=Model/Admin/Plan --resource

回答by realtebo

This works for actual Laravel version, 5.6.28, on Windows 7

这适用5.6.28于 Windows 7 上的实际 Laravel 版本

php artisan make:model App\Models\NewModel

Note: Do not use double escapes ('\\')

注意:不要使用双转义符 ( '\\')

This generate the file App\Models\NewModel.phpas follows

这将生成App\Models\NewModel.php如下文件

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class NewModel extends Model
{
    //
}