I
I
Ivan2019-02-14 17:35:19
Laravel
Ivan, 2019-02-14 17:35:19

How to build relationships?

there is a relationship between the tables

return $this->hasManyThrough(
      Catalog_generation::class,
      Catalog_generation_body::class,
      'catalog_body_id',
      'id',
      'id',
      'catalog_generation_id'
    )->select([
      "catalog_generations.id",
      "catalog_generations.catalog_model_id",
      "catalog_generation_bodies.catalog_body_id",
    ])
      ->with(['model.brand']);

with the relation of Catalog_generation to other tables, I need to use in the same width the relation of Catalog_generation_body Question: how to solve such a problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jazzus, 2019-02-14
@jazzus

Author, it's really hard to understand what you have in mind. But I would change the names. Models are named as ModelModel. Tables for models model_models. Otherwise it's easy to get confused. Especially with hasManyThrough. Also relations separately, DB queries based on relations - separately. relations can be useful to you in other queries. As for the relationship, you can try this (if I'm not confused)

return $this->hasManyThrough(
                        Catalog_generation::class,
                        Catalog_generation_body::class,
                        'current_model_id', //Внеший ключ текущей модели в Catalog_generation_body
                        'catalog_generation_body_id', // Внешний в Catalog_generation
                        'id' // Локальнй в текущей модели
                      )

The current model is the model in which you build these relationships..)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question