How to make relationship using the result of a relationship with Eloquent

Asked

Viewed 45 times

1

I’m having trouble making the relationship between the table produtos, opções and tipos de opções on Laravel, I can bring the products and their options using Product::with('options')->get(), but I wanted the options to come with the guys too.

I do not know if the mistake is in the Bank or in the relationship of the Model, because in the Bank I can bring normally using Join.

Observing:

Each product can have several options which in turn has several types.

Examples of options:

  • "Size",
  • "Coloured",
  • "Heaviness".

Examples of Types of options:

  • "P",
  • "Red",
  • "1 kg".

Tables:

Relacionamento das tabelas

Model Product:

public function options()
{
        return $this->belongsToMany('App\ProductsOptions',
                  'products_has_options',
                  'id_product',
                  'id_option')
          ->withPivot('id');
}

Model Productsoptions:

public function products_options_types()
{
    return $this->belongsToMany('App\ProductsOptionsTypes',
                     'products_has_options_types',
                     'id_product_has_option',
                     'id_options_types');
}
  • Example: https://answall.com/questions/376317/laravel-5-8-podemos-fazer-sync-em-uma-tabela-pivot-sem-o-campo-id/376867#376867

  • Example: https://answall.com/a/376867/54880

  • Example: https://answall.com/questions/244308/laravel-eloquent-consulta-em-mais-de-uma-tabela/244376#244376

  • These fields id are only for auto - increment in many to many junctions? if they are are unnecessary

  • So I was using the id of the products_has_options table in the id_products_has_option field of the products_has_options_types table because I needed to relate the type of option to the product option

  • I understood, so it’s a little different all these relationships and you have to create a Model Entity for each table with primary key and everything. Have you implemented anything?

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.