B
B
BonBon Slick2017-10-08 15:36:56
SQL
BonBon Slick, 2017-10-08 15:36:56

Selecting products from categories that have a similar slug, what subquery is needed in join?

Purpose: to select a category name by slug for localization. For example, a user has requested a product with the Phone category and the product from this category will be returned to him, however, some products have been added to the Phone category , that is, there are 2 categories. The structure in the database is:

['lang' => 'en', 'name' => 'Phone', 'slug' => 'phone-en'],
  ['lang' => 'ru', 'name'=>'Телефон', 'slug' => 'phone-ru'],

Therefore, there are 2 categories. It is possible that this approach is not correct, perhaps it is worth creating another table in which there will be localized category files. And pull from there already by the lang parameter .
However, now the situation is like this, here is such a join with a subquery:
->join('categories', function ($join) use ($localLang) {
                    $join->on('products.categorie_id', '=', 'categories.id')
                        ->where('categories.slug', 'ILIKE', '%?????????%');
                })

If put
->where('categories.slug', 'ILIKE', '%' . $localLang . '%'); // ru

will return the product only that was created with Russian localization, the rest of the product will be missed. since the selection will be products from all where there is a -ru prefix in the category slug field.
Can you please tell me how to get the current category for each product in a subquery to use a select?
Something like this would be:
->where('categories.slug', 'ILIKE', '%' . $categoryOfCurrentProduct . '%'); // вместо ru будет phone

And then it will select all the goods from the categories where the slug has a phone , regardless of the localization ru/en/ch/it...
Perhaps then it will be necessary to group everything somehow. For then each product, for example, the phone category, will be for each localization. That is, if 7 languages ​​\u003d 7 products with id 1 product, for each category translation.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
d-stream, 2017-10-08
@BonBonSlick

Isn't it easier in this context and the curvature of the architecture to cling to the fact that allegedly different slugs (xxx-ru, xxx-en, etc.) are the same in terms of the left part before the hyphen?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question