带下划线的 Laravel 模型和表命名

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

laravel model and table naming with underscore

phplaravel

提问by Orbitum

I am trying to create model named CustomDataStore(models/custom_data_store.php) and it is extending Eloquent, so table is named as custom_data_stores, but it gives me error.

我正在尝试创建名为CustomDataStore( models/custom_data_store.php) 的模型并且它正在扩展 Eloquent,因此表被命名为custom_data_stores,但它给了我错误。

Eloquent wants table named customdatastores. Of course I can set manually table name, but how can I set such name automatically?

Eloquent 想要名为customdatastores. 当然我可以手动设置表名,但是如何自动设置这样的名称?

回答by William Cahill-Manley

For it to be made automatically your model would have to be in models/custom/data/store.php

要自动制作它,您的模型必须在 models/custom/data/store.php

class Custom_Data_Store extends Eloquent
{

}

回答by Codemunkeee

I have noticed that laravel's eloquent can't really "see" if there are underscores on the model's filename. I'm not really sure what's happening inside (still a newbie), but this is only based on my observation..

我注意到 laravel 的 eloquent 无法真正“看到”模型文件名上是否有下划线。我不太确定里面发生了什么(仍然是新手),但这只是基于我的观察..

what I did was

我所做的是

I have two different models, named tblReport_date.phpand tblReportdate.php, both have the same codes except that they are pointed at different tables.

我有两个不同的模型,命名为tblReport_date.phptblReportdate.php,它们具有相同的代码,只是它们指向不同的表。

tblReportdate.phpcode:

tblReportdate.php代码:

<?php

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class tblReportdate extends Eloquent implements UserInterface, RemindableInterface
{
    /**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'tblClients';

    ...rest of the codes...

tblReport_date.phpcode:

tblReport_date.php代码:

<?php

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class tblReportdate extends Eloquent implements UserInterface, RemindableInterface
{
    /**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'tblReport_date';

    ...rest of the codes...

on the controller, I have this code

在控制器上,我有这个代码

$db = tblReportdate::all();
return View::make('db.index')->with('db', $db);

the result is that it will only load tblReportdate.phpand not tblReport_date.php

结果是它只会加载tblReportdate.php而不是tblReport_date.php

I tried extracting tblReport_date.phpalone and tested it.. it always returns an error, regardless of the class name, etc..and IDK why. If someone can explain this pls comment it out.. anyways, just avoid putting underscores on filenames XD

我尝试tblReport_date.php单独提取并测试它..它总是返回错误,无论类名等..和 IDK 为什么。如果有人可以解释这个请把它注释掉..无论如何,只要避免在文件名上放置下划线XD