Translate search where

Hi guys!

I need to do this query:

Product::where('name', 'like', "%${query}%")->get();

but for the translated model. Something like this:

Product::transWhere('name', 'like', "%${query}%")->get(); //This didn't work 

Any ideas, how to do this?
Thanks

Hi,

I already use transWhere function in my code,
You have to declare the column as an index in your $translatable array in your model. you can have more informations on the github page here : GitHub - rainlab/translate-plugin: Enables multi-lingual sites

Best,

Hi, I know but transWhere works only with equals query, but I need query with LIKE comparision.

If you search inside PHP file in the repository, you can find this :

 /**
     * scopeTransWhere applies a translatable index to a basic query. This scope will join the
     * index table and can be executed neither more than once, nor with scopeTransOrder.
     * @param  Builder $query
     * @param  string $index
     * @param  string $value
     * @param  string $locale
     * @return Builder
     */
    public function scopeTransWhere($query, $index, $value, $locale = null, $operator = '=')
    {
        return $this->transWhereInternal($query, $index, $value, [
            'locale' => $locale,
            'operator' => $operator
        ]);
    }

So, it’s written that you can add an operator as a 4th parameter.

i didn’t test it, but it seems logic to me. Try it and let us know :slight_smile:

Best,

3 Likes

Yeah, its working - 4th param is operator:

Product::transWhere('slug', "%${query}%",null,'like')->get();
2 Likes