dongyao4003 2016-08-29 12:25
浏览 38
已采纳

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

From the doc of laravel, it claims:

Now, let's look at an example Flight model, which we will use to retrieve and store information from our flights database table

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:

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. 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
  • PHP 7.0.8-2+deb.sury.org~xenial+1
  • 写回答

3条回答 默认 最新

  • doumi1852 2016-08-29 12:35
    关注

    As @FrankProvost said, Laravel (Doctrine, actually) is smart enough and has hardcoded words that should not be inflected:

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

    So, use media table or use protected $table variable to force medias table name.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看