php Laravel 5 模型文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29687412/
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-26 00:31:20  来源:igfitidea点击:

Laravel 5 Models Folder

phplaravelmodelartisan

提问by Alan

I have a directory called modelsinside app/where I place all my model classes. Now I want to change the directory where the following command outputs the generated classes

我有一个名为modelsinside的目录app/,用于放置所有模型类。现在我想更改以下命令输出生成的类的目录

php artisan make:model SomeModel

回答by Alan Storm

You can't and, if you're hewing to Laravel's version of the universe, you shouldn't. Laravel 5 provides, and assumes, a PSR-4 naming convention for all its class files. This means models (and all classes) should be placed based on their full class name.

你不能,而且,如果你正在使用 Laravel 的宇宙版本,你不应该。Laravel 5 为其所有类文件提供并假定了 PSR-4 命名约定。这意味着模型(和所有类)应该根据它们的完整类名放置。

When you say

当你说

php artisan make:model SomeModel

You're actually creating a model with the full name of App\SomeModel, so artisancreates the following file.

您实际上是在创建一个全名为 的模型App\SomeModel,因此artisan创建了以下文件。

app/SomeModel.php

If you said

如果你说

php artisan make:model 'Test\SomeModel'

you'd be creating a model with the full name App\Test\SomeModel, and Laravel would create the following file

您将创建一个具有全名的模型App\Test\SomeModel,Laravel 将创建以下文件

app/Test/SomeModel.php

So, its your model's full class name (namespace included) that determines where the class definition file is.

因此,您的模型的完整类名(包括命名空间)决定了类定义文件的位置。

回答by Travis Yang

I found it should be:

我发现它应该是:

php artisan make:model ModelFolder\SomeModel

回答by Indra Darmawan

If you want to create Models manually on specific folder. Example if you want to create model infointo folder Models. Create file named info.phpinside folder Models that you created before. Here are you should write code like this in info.php

如果要在特定文件夹上手动创建模型。例如,如果要将模型创建info到文件夹Models. info.php在您之前创建的文件夹 Models 内创建名为的文件。这里是你应该写这样的代码info.php

<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Info extends Model
{
    // your code
    protected $table = 'tb_users';
}

Of course you also can use Artisan command php artisan make:model "Models\Info"

当然你也可以使用 Artisan 命令 php artisan make:model "Models\Info"

If you want to call that model Infoyou create a view

如果要调用该模型Info,则创建一个视图

<?php
$users = App\Models\Info::all();

foreach ($users as $user) {
    echo $user->name;
}

It will display all name of users

它将显示所有用户名称

回答by Phil Unamka

You could also look at www/yourfolder/database/php file created when the model was created.

您还可以查看创建模型时创建的 www/yourfolder/database/php 文件。

回答by bangnokia

If you too lazy and don't want to type the word Modelsevery time, you can try this my https://github.com/bangnokia/laravel-models-folder

如果你太懒不想Models每次都打字,可以试试这个我的https://github.com/bangnokia/laravel-models-folder

回答by Chukwuemeka Ihedoro

Look into this using this tutorial on medium https://medium.com/@codingcave/organizing-your-laravel-models-6b327db182f9. Its a better solution to extend the ModelMakeCommandso that instead of php artisan make:model 'Test\SomeModel'you will run php artisan make:model SomeModeland you'll still get 'Test\SomeModel'namespace with SomeModel.phpin 'Test'folder

使用本教程在中等https://medium.com/@codingcave/organizing-your-laravel-models-6b327db182f9上查看此内容。它是一个更好的解决方案来扩展,ModelMakeCommand这样php artisan make:model 'Test\SomeModel'你就不会运行php artisan make:model SomeModel,你仍然会在文件夹中获得'Test\SomeModel'命名空间SomeModel.php'Test'