简体   繁体   English

Laravel5:为什么我的模型“媒体”需要表名为“媒体”而不是“媒体”?

[英]Laravel5: Why my model “Media” require table named “media” instead of “medias”?

From the doc of laravel , it claims: laravel文档中 ,它声称:

Now, let's look at an example Flight model, which we will use to retrieve and store information from our flights database table 现在,让我们看一个示例Flight模型,我们将使用该模型从flights数据库表中检索和存储信息

So I wrote: 所以我写道:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Media extends Model
{
    //
}

And controller: 和控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Media;

class MediaController extends Controller
{
    public function index() {
        return view('medias.index')->with('medias', Media::all());
    }
}

But when I query MediaController@index , it gives that error: 但是当我查询MediaController@index ,它给出了该错误:

QueryException in Connection.php line 729:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'myapp.media' doesn't exist (SQL: select * from `media`)

It requires a table named media instead of medias as document said. 它需要一个名为media的表,而不是文档所述的medias So why that errors happened? 那么为什么会发生这种错误呢? Do I accidentally open option flag which changed the default name mapping from model to table or something similar? 我是否意外打开了将默认名称映射从模型更改为表格或类似名称的选项标志?

Any suggestion will be appreciated. 任何建议将不胜感激。


Environment: 环境:

  • Laravel Framework version 5.2.43 Laravel Framework版本5.2.43
  • PHP 7.0.8-2+deb.sury.org~xenial+1 PHP 7.0.8-2 + deb.sury.org〜xenial + 1

As @FrankProvost said, Laravel (Doctrine, actually) is smart enough and has hardcoded words that should not be inflected : 就像@FrankProvost所说的那样,Laravel(实际上是Doctrine)足够聪明,并且具有不应该变形的硬编码单词

....
'Kongoese', 'Lucchese', 'mackerel', 'Maltese', '.*?media',
....

So, use media table or use protected $table variable to force medias table name. 因此,请使用media表或使用protected $table变量强制medias表名称。

If you want that your table is named "medias" in your model "media", try to specify in Class. 如果希望在模型“ media”中将表命名为“ medias”,请尝试在Class中指定。

class Media extends Model
{
    protected $table = 'medias';
}

If you want that your table is named "medias" in your model "media", you need to define protected variable like this 如果要在模型“ media”中将表命名为“ medias”,则需要定义这样的受保护变量

class Media extends Model
{
   protected $table = 'medias';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM