wanjydan
wanjydan

Reputation: 139

Laravel looking for the wrong table

I have specified the table name in the model class. 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'];
}

This is my migration:

class CreateSizeProductTable extends Migration
{

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

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

But i still get this error:

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

Upvotes: 0

Views: 364

Answers (1)

Parisa
Parisa

Reputation: 801

maybe sizeproduct is your pivot table for manytomany relation, so as described here, The pivot table is derived from the alphabetical order of the related model names (so in your case product comes first).

You can change it in relation defining code:

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

Or you may prefer to create separate migration for your pivot table.

Upvotes: 2

Related Questions