Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kfirba's avatar
Level 50

Eloquent and 3 way m-m relationship

Hello!

I have the following tables:

  • recipes
  • ingredients
  • units
  • recipes_ingredients

My recipes_ingredients table has 3 columns:

  1. recipe_id
  2. ingredient_id
  3. unit_id

This table connects the ingredients, the unit the recipe should be measured with, and the recipe which the ingredient belongs to.

Now with Eloquent I know I can do a 2 way m-m relationship. However, as you may have guessed, it doesn't suit my needs. Is there any way I can do a 3 ways m-m relationship with Eloquent? It doesn't have to be a 3rd package, you can also show a snippet of how I would do that (How would I retrieve all the ingredients with their units for a certain recipe, how would I attach a unit/ingredient to a recipe etc.)

Thanks!

0 likes
10 replies
kfirba's avatar
Level 50

@blackbird I'm not sure how eager loading would help me here? There isn't a nested relationship here which I can use this eager loading with. I need to return the ingredient actual name plus the chosen unit for the ingredient for a particular recipe.

bobbybouwmann's avatar

Well if you setup your many to many relations then you can nest them right when retrieving? I hope I understand you correct now!

Movie.php

class Movie extends Model {

    ...

    public function actors()
    {
        return $this->belongsToMany('App\Actor');
    }

    public function genres()
    {
        return $this->morphMany('App\Genre', 'genreable');
    }

    public function producer()
    {
        return $this->belongsTo('App\Producer');
    }
}

Then in your controller you can do something like this

public function all()
{
    return Movie::with(['actors', 'producer', 'genres'])->get();
}
bobbybouwmann's avatar

Cool package didn't know that! I think that will do the trick then ;)

kfirba's avatar
Level 50

@bestmomo it doesn't seem that it has been updated to laravel 5 yet. I will take a look at the source code so maybe I can figure out what was done there.

bestmomo's avatar

@kfirba I didn't try it on L5 so I cant say but I think i'll work easily and JarekTkaczyk often looks at this forum to help.

JarekTkaczyk's avatar

@kfirba In fact I created the feature, but it was joechilds, who made it a package. That said, I'm not sure, but I suppose there's nothing to adjust for L5 there, so just try it out.

If it doesn't work, then let me know and I will take a look. I'm working on something bigger, kinda eloquent extensions currently.

kfirba's avatar
Level 50

@JarekTkaczyk If your time estimation for that eloquent extension is over the corner, I wouldn't mind waiting. I will try the package and let you know somewhen this/next week

sorendam's avatar

@kfirba did you figure out how to make that call to get a recipe with all its ingredients with the unit? I'd really ikke to know how to do this as I have the exact same issue.

Please or to participate in this conversation.