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

2022-08-30 10:09:03

当我尝试使用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))

这是我的控制器存储方法:

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

这是我的模型:

<?php namespace App\Models\Cotizacion;

use Illuminate\Database\Eloquent\Model;


class Cotizacion extends Model {

}

我必须忽略一些非常明显的东西,因为我不明白为什么Laravel要添加一个“S”,表格是cotizacion而不是cotizacions。

如何排查此问题?


答案 1

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

只需在模型中指定您的表,如下所示:

class Cotizacion extends Model{
    public $table = "cotizacion";

答案 2

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

Schema::table('table_name', function ($table)  {
    // ...
});

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

Schema::create('table_name', function ($table)  {
    // ...
});

推荐