I have a database with the following tables:
products
id
name
materials
id
name
units
id
name
products and materials table has many to many relationships, also materials and units table have many to many relationships as well so these are 2 pivot tables
material_product
material_id
product_id
material_unit
material_id
unit_id
Now from these relations, I know about the material that is used in the product but how will I find out the associated unit of each material as there are many to many relations between materials and units table.
For example, if there is a Product-A
which has Aluminum
as one of the material and Aluminum
is linked to both Kg
and Ton
on units table. Now how will I get the correct unit of Aluminum
when I access the Product-A
.
Can I do this in Laravel, instead of making materials and products relation I relate products table to material_unit pivot table as such
material_unit_product
product_id
material_unit_id
and yes then there will be an increment field in material_unit table as well which will act as id so material_unit table will be
material_unit
id
material_id
unit_id
unique([material_id, unit_id)]
Or can I use the id from the material_unit pivot table in the material_product pivot table as an extra field?
Or is this a better way to do this type of relationship in Laravel? I don't know how to do this properly, any help will be much appreciated.
Thanks
material_unit_id
andproduct_id
inmaterial_product
. Depending on what you are storing that is. I would consider thematerial_unit
a list of possible units to use for this material, andmaterial_unit_product
a list of concrete material units that belong to a product. You might even consider a table with all 3 id's in them:material_id
,unit_id
,product_id
. This way you simply say: This product has this material in this unit. – Rob Biermannmaterial_unit_id
inmaterial_product
pivot table? But I don't think it is the best approach. – MAYAluminum
sometimes containliter
unit ? non sense right ? – Elbo Shindi Pangestuid
to material_unit pivot table we can useid
of units table and link it to material_product pivot table, in that way we can avoid the creation of material_unit pivot table. – MAY