I'm using Laravel 5. I have created a /Models
directory under the /App
directory, but when generating the models using Artisan it's storing them under the App
directory.
我正在使用Laravel 5.我在/ App目录下创建了一个/ Models目录,但是当使用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 artisan
to save the model to specific directory?
如何指示工匠将模型保存到特定目录?
5 个解决方案
#1
7
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 --path
option like so:
如果要在生成模型时指定路径,可以使用Laravel Generators Package。然后,您可以使用--path选项指定位置,如下所示:
php artisan generate:model TestModel --path=my/custom/location
#2
90
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
然后,您只需键入artisan命令即可在Models目录中创建模型
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
#3
18
For those using 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
php artisan model:make App\\Models\\Foo
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.php
file in the app/Models directory and update the namespace accordingly.
上面的命令将在app / Models目录中创建Foo.php文件并相应地更新名称空间。
Hope that helps.
希望有所帮助。
#4
10
In Laravel 5.4 you can create with
在Laravel 5.4中你可以创建
> php artisan make:model "Models\userModel"
here Models is directory name and userModel is model name
here是目录名称,userModel是型号名称
Use " (double quotes) or ' (single quotes) to create model
使用“(双引号)或'(单引号)来创建模型
#5
0
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
#1
7
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 --path
option like so:
如果要在生成模型时指定路径,可以使用Laravel Generators Package。然后,您可以使用--path选项指定位置,如下所示:
php artisan generate:model TestModel --path=my/custom/location
#2
90
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
然后,您只需键入artisan命令即可在Models目录中创建模型
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
#3
18
For those using 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
php artisan model:make App\\Models\\Foo
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.php
file in the app/Models directory and update the namespace accordingly.
上面的命令将在app / Models目录中创建Foo.php文件并相应地更新名称空间。
Hope that helps.
希望有所帮助。
#4
10
In Laravel 5.4 you can create with
在Laravel 5.4中你可以创建
> php artisan make:model "Models\userModel"
here Models is directory name and userModel is model name
here是目录名称,userModel是型号名称
Use " (double quotes) or ' (single quotes) to create model
使用“(双引号)或'(单引号)来创建模型
#5
0
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