Skip to content
Advertisement

Change Primary ID in laravel Migration

I’m creating a table where I want to use the ID from this table “menu_items” to be added in another table

Table one: menu_items

JavaScript

Table 2 products

JavaScript

My aim of doing this is that I can then create a relationship between the 2 tables as they have the same key?

Is there a different approach I can take? Should I create a unique key when creating a menu item and then add this to the second table?

Thank you.

Advertisement

Answer

Basically Laravel Eloquent do the key handling. When you have two tables which both has as key the name id, this is not a problem. Name the keys in the relation table just like this

JavaScript

Laravel will handle this in Eloquent. You are also able to name the two columns in the relation table to what ever you want. You could define it for the relation in Eloquent. E.g.

JavaScript

Please have a look into: Laravel Relationship Documentation

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement