未找到基准表或视图:1146表Laravel 5

ray*_*n0w 39 php mysql laravel

我尝试使用Laravel 5将数据保存到mysql时出现此错误,其他表单和save()方法工作正常,但这一个:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'sistemal5.cotizacions' doesn't exist (SQL: insert into `cotizacions` (`customer_id`, `total`, `updated_at`, `created_at`) values (1501, 150.69, 2015-05-11 03:16:25, 2015-05-11 03:16:25))
Run Code Online (Sandbox Code Playgroud)

这是我的Controller存储方法:

public function store(CotFormRequest $request)
    {    
        $quote = new Cotizacion;
        $quote->customer_id = Input::get('data.clientid');
        $quote->total = Input::get('data.totalAftertax');    
        $quote->save();    
    }
Run Code Online (Sandbox Code Playgroud)

这是我的模型:

<?php namespace App\Models\Cotizacion;

use Illuminate\Database\Eloquent\Model;


class Cotizacion extends Model {

}
Run Code Online (Sandbox Code Playgroud)

我必须忽略一些非常明显的东西,因为我无法理解为什么Laravel正在添加一个"S"表格是cotizacion而不是cotizacions.

我怎么解决这个问题?

Jam*_*nce 93

我猜Laravel无法确定您用于表名的复数形式.

只需在模型中指定您的表格:

class Cotizacion extends Model{
    public $table = "cotizacion";
Run Code Online (Sandbox Code Playgroud)

  • 这是Laravel的标准.默认情况下,它们会复数表名称(即模型`Business`用于表`business`).如果您不喜欢,则需要手动指定表名. (11认同)
  • 问题是laravel正在添加'S',为什么? (6认同)
  • 那是因为设备的复数是设备 (4认同)
  • 如果您正在寻求澄清,请在[Eloquent docs here]中解释(http://laravel.com/docs/5.0/eloquent#basic-usage) (2认同)

小智 6

我在laravel 5.2中也遇到了这个问题,如果声明表名不起作用,可能是因为你在Request中的验证代码中有一些错误的声明或错误(如果你使用的话)


Ina*_*man 6

检查您的迁移文件,也许您正在使用Schema :: table,如下所示:

Schema::table('table_name', function ($table)  {
    // ...
});
Run Code Online (Sandbox Code Playgroud)

如果要创建新表,则必须使用Schema :: create:

Schema::create('table_name', function ($table)  {
    // ...
});
Run Code Online (Sandbox Code Playgroud)