Laravel 找错了 table

Laravel looking for the wrong table

我在模型 class 中指定了 table 名称。 Laravel 5.6, PHP 7

namespace App;

use Illuminate\Database\Eloquent\Model;

class SizeProduct extends Model

{
    protected $table = 'size_product';

    protected $fillable = ['product_id', 'size_id'];
}

这是我的迁移:

class CreateSizeProductTable extends Migration
{

public function up()
{
    Schema::create('size_product', function (Blueprint $table) {
        //some code here
    });
}

public function down()
{
    Schema::dropIfExists('size_product');
}

但我仍然收到此错误:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db_name.product_size' doesn't exist

可能 sizeproductmanytomany 关系的枢轴 table,因此如此处所述,枢轴 table 派生自 相关型号名称的字母顺序(因此在您的情况下,产品排在第一位)。

您可以在关系定义代码中更改它:

public function products()
{
    return $this->belongsToMany(Size::class,'size_product');
}

或者您可能更愿意为您的枢轴创建单独的迁移 table。