g3tl0st
g3tl0st

Reputation: 883

(errno: 150 "Foreign key constraint is incorrectly formed") in many-to-many relationship in Laravel

I'm try to create many-to-many that link with customer and shop in laravel but stuck in this error (errno: 150 "Foreign key constraint is incorrectly formed") and still, not figure it out.

Here my customers table

Schema::create('customers', function (Blueprint $table) {
  $table->bigIncrements('id');
  $table->unsignedBigInteger('shop_id');
  $table->string('name');
  $table->string('email');
  $table->string('phone');
  $table->timestamps();

  $table->foreign('shop_id')->references('id')->on('shop');
});

Here my shops table

Schema::create('shops', function (Blueprint $table) {
  $table->bigIncrements('id');
  $table->unsignedBigInteger('customer_id');
  $table->string('name');
  $table->timestamps();

  $table->foreign('customer_id')->references('id')->on('customer');
});  

My Shop Model

protected $fillable = ['name'];

public function customer()
{
  return $this->belongsToMany(\App\Customer::class);
}

My Customer Model

protected $fillable = ['name', 'email', 'phone'];

public function shop()
{
  return $this->belongsToMany(\App\Shop::class);
}

Any Help? Thanks in advances....

Upvotes: 0

Views: 204

Answers (4)

Mahdi Safari
Mahdi Safari

Reputation: 298

The relation of many-to-many need third table pivot table which you miss it.
for do that create new migration.
php artisan make:migration create_customer_shop

there is no need create model for pivot table

then you Schema pivot table something like this.
Pivot table

Schema::create('cutomer_shop', function (Blueprint $table) {
  $table->increments('id');
  $table->integer('customer_id')->unsigned();
  $table->integer('shop_id')->unsigned();
  $table->timestamps();

  $table->foreign('customer_id')->references('id')->on('customers');
  $table->foreign('shop_id')->references('id')->on('shops');
});  

The two table shops and customers doesn't have any direct relations they have relation by just pivot table.
Note: make sure all three table id type increments('id') and all foreign key are $table->integer('shop_id')->unsigned(); otherwise give you incorrect format error.

Upvotes: 0

Jithesh Jose
Jithesh Jose

Reputation: 1814

Put the foreign key in relationship rather than in migration.

 public function customer()
 {
  return $this->belongsToMany('\App\Customer::class','id','shop_id');
 }

 public function shop()
 {
   return $this->belongsToMany('\App\Shop::class','id','customer_id');
 }

Upvotes: 0

Ripon Uddin
Ripon Uddin

Reputation: 714

Note for you. Don't use foreign command in table create command together.
Make sure always new migration file for adding foreign key's inside table.
Cause sometime its generate Errors while migrating .. open your bash shell or PHP Storm Terminal or CMD

php artisan make:migration foreign_customer_id_at_table_shops --table=shops //you Can use your own migration name what you want

at foreign_customer_id_at_table_shops migration file

UP

$table->foreign('customer_id')->references('id')->on('customers');

DOWN

$table->dropForeign(['customer_id']);

Upvotes: 0

nayeemdev
nayeemdev

Reputation: 1241

Check your Schema - Its should be shops not shop...

$table->foreign('shop_id')->references('id')->on('shops');

and similarly customers not customer...

$table->foreign('customer_id')->references('id')->on('customers');

Upvotes: 3

Related Questions