【问题标题】:laravel model and table naming with underscorelaravel 模型和表命名带下划线
【发布时间】:2012-12-14 03:00:07
【问题描述】:

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

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

【问题讨论】:

  • 这是 Laravel 命名约定。如果您不喜欢它,也许您可​​以使用自定义表名解析扩展核心 Eloquent 模型,以便与您的其他模型一起使用。它在laravel/database/eloquent/model.php 中的table() 方法中

标签: php laravel


【解决方案1】:

要自动生成,您的模型必须位于 models/custom/data/store.php

class Custom_Data_Store extends Eloquent
{

}

【讨论】:

  • 但是模型会用下划线命名。不过我会试试的。
【解决方案2】:

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

我做的是

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

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.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...

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

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

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

我尝试单独提取tblReport_date.php 并对其进行测试.. 它总是返回错误,无论类名如何等等..以及 IDK 为什么。如果有人可以解释这一点,请将其注释掉。无论如何,请避免在文件名上加下划线 XD

【讨论】:

    猜你喜欢
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    • 2015-10-07
    相关资源
    最近更新 更多