6 months ago

Many to many relationship, same table?

Posted 6 months ago by heykatieben

I'm trying to model ingredients, that contain many other ingredients - either "may contain", or "always contains" (many to many with a relationship type defined). I'm having trouble figuring out how to model this, especially in Nova.

Right now my table is ingredient_ingredient, with columns source_ingredient_id, dest_ingredient_id, and relationship (enum "always contains", "may contain"). I have this in my base model:

  public function always_contains() {
    return $this->belongsToMany('App\Ingredient', 'source_ingredient_id', 'dest_ingredient_id')->where("relationship", "Always Contains");

  public function sometimes_contains() {
    return $this->belongsToMany('App\Ingredient', 'source_ingredient_id', 'dest_ingredient_id')->where("relationship", "Sometimes Contains");

In the Nova model, I have this:

      BelongsToMany::make('Sometimes Contains', 'sometimes_contains', 'App\Nova\Ingredient'),
      BelongsToMany::make('Always Contains', 'always_contains', 'App\Nova\Ingredient') 

Any pointers?

Please sign in or create an account to participate in this conversation.