Ahmad Badpey
Ahmad Badpey

Reputation: 6612

search in appropriate Mysql fields based on current App Locale in laravel

In a multilingual website I have these fields for Products table

product_id
title_fa
title_en
title_ar
desc_fa
desc_en
desc_ar
.
.
.

Now I want to search in (for Example) title fields based on Current laravel Locale. I know that we can use this way :

$products   =   Product::where('title_'.App::getLocale(),'LIKE' , '%'.$data['search_value'].'%');

But I thinks that is not clean and easy for number of large fields is not suitable.

I'm looking for a way that was and simple as normal search field and then laravel search based on that field in background According to current App Locale like:

$products   =   Product::where('title','LIKE' , '%'.$data['search_value'].'%');

How Can I do that and what is best way ?

Upvotes: 3

Views: 380

Answers (1)

umefarooq
umefarooq

Reputation: 4574

Hi if you want really easy way for search products than use this following laravel multi language package

https://github.com/dimsav/laravel-translatable

in this package you have to create 2 tables one Product and Product_translate, product table will have your basic product info, translate will have language data, in product_translate table you can use search queries, ease is table column name will remain same in where you have use language filter.

Upvotes: 1

Related Questions