D
D
Dmitry2020-11-28 15:46:28
MySQL
Dmitry, 2020-11-28 15:46:28

How best to structure the links of the three models (a la price)?

I continue to rewrite the old project on laravel. In an attempt to optimize everything complicated by tradition.

There are 3 models. city, category, service. Roughly speaking price list.
City-Category everything is clear here. Many to Many via add. table. And then the shutdown begins.
category service. made One to Many. It seems logical - each service belongs to some category. In this case, the category is tied to the city.
And now there is a situation that in different cities the cost of the same service may be different.
And I can not figure out in any way what to tie where in this case.

Tried to do

public function services()
    {
        return $this->hasManyThrough(
            Service::class,
            Category::class,
        );
    }

But it is logical that it does not find the city_id field in the Category model, since the relationship goes through many to many.

On idea the table of communications - City-Service is necessary still? many to many with an additional price field.

I used to do all of this before. Table with categories (category, city). Table with services (category_id, city).
And manually create categories for each city. Then, in each city, in each category, add the same services. 5 years ago it was 2 cities. Now in general it is necessary to complicate everything.

I will be grateful for advice and where it is possible to peep similar implementations.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pLavrenov, 2020-11-29
@pLavrenov

MAnyToMany obviously requires a pivot table,
many-to-many
many-to-many-polymorphic-relations

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question