-2

I am grateful in advance for any answer!

there are CATEGORIES

  • CATEGORIES (one) 1⟶n PRODUCTS (many)
  • PRODUCTS (many) m⟶n STORE (one)
  • STORES (one) 1⟶n ADDRESS_CITY (many)

CATEGORIES → PRODUCTS → STORES → ADDRESS_CITY

you must select all the CATEGORIES that have:

  • there is at least 1 PRODUCT / / has
  • this PRODUCT must have a STORE in which the ADDRESS exists in California

I tried to build a query, it does not work, there is only the hasManyThrough method in the documentation
that jumps through 1 table, and there are more of them here. Or I'm even being stupid)
Help to build a query!

Another question: if I have 1 million products and 1000 stores, will the request be processed normally or do I need to look for other FEATURES:

  • save the id_city in JSON in the PRODUCTS
  • save the id_city in JSON in the STORE
shaedrich
  • 5,457
  • 3
  • 26
  • 42
Den
  • 27
  • 5
  • 1
    What have you tried so far ? wich problems did you encounter in your tries ? Keep your post about only one question. – N69S Jul 15 '21 at 11:57
  • @N69S it is not possible to link all the tables through hasManyThrough, is there another way? – Den Jul 15 '21 at 12:00
  • You dont need to make complicated relation to condition any of those entities with any other entity. Just conventional relations work perfectly `belongTo`, `hasMany` only – N69S Jul 15 '21 at 12:11
  • 2
    You can do it with nested `whereHas()` https://laravel.com/docs/8.x/eloquent-relationships#querying-relationship-existence – N69S Jul 15 '21 at 12:19
  • @N69S please tell me if it is possible to put has() in ->whereHas ('product – Den Jul 15 '21 at 13:04

1 Answers1

0
$result = Category::where('on_off', 1)
    ->whereHas('product', function ($query) use ($city_one) {
        $query->where('on_off', 1)

            ->whereHas('shop', function ($query) use ($city_one) {
                $query->where('on_off', 1)

                    ->whereHas('shop_map_point', function ($query) use ($city_one) {
                        $query->where('city_id', $city_one->id);
                    });
            });
    })
    ->get();
N69S
  • 16,110
  • 3
  • 22
  • 36
Den
  • 27
  • 5