Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

I think that is a good idea to add another table, because the number of units is limited.
You can find meny threads about a four table relation. It's difficult to make a select query for it, but if you add a model for the connecting table it becomes simple. You already have an id in the ingredient_recipe table. Make a model for that table.
You will be able to eager load them simply. Recipe::find($id)->with('connectors', 'connectors.ingredient', 'connectors.unit');

Last updated 2 years ago.
0

Thanks for your reply!

I'm not sure if I understood right ...

  • Create a 'units' table
  • Create a 'ingredient_recipe_unit' pivot table where I assign a specifig ingredient entry of a specific recipe to a specific unit
  • Create a 'Units' model which you named "Connectors" in your example
  • Create a 'Ingredients_Recipe' Model
  • Add the relationships to those models ( one ingredient_recipe can have one unit AND one unit can have many ingredient_recipe -> so it's a one to many relationship)

Is that right?

Sorry but I'm a bit confused ... i've never seen a model for a pivot table or something like that so I don't know how to go about that... I'm relatively new to laravel and I would appreciate some kind of help a lot! event if it's a good read or something like that :) ...

Thanks!

Last updated 2 years ago.
0

You misunderstood me.
The only table you will add is your units table. Nothing else. The char(50) unit column in the existing pivot table should be changed into an integer as foreign key (unit_id). That table will connect al three models at the same time. The 'Connector' I am talking about should be an 'Ingredient_Recipe' model.
Ther relationships you described are good.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Ptrckolous ptrckolous Joined 12 Nov 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.