nicolaslopezj / searchable

A php trait to search laravel models
MIT License
2.01k stars 291 forks source link

How to search on column translatable #199

Open ikkosatrio opened 5 years ago

ikkosatrio commented 5 years ago

i use this library https://github.com/Astrotomic/laravel-translatable/ how to search on colum translatable?

Eskiell commented 5 years ago

I no know.

timopaul commented 4 years ago

Does anyone have a solution? I am facing the same problem.

So far my solution is via the join configuration:

    /**
     * Searchable rules.
     *
     * @var array
     */
    protected $searchable = [
        'columns' => [
            'article_translations.title' => 10,
            'article_translations.keywords' => 8,
            'article_translations.description' => 2,
        ],
        'joins' => [
            'article_translations' => [
                'articles.id', 'article_translations.article_id',
            ],
        ],
    ];

But all translations are always returned and so I have each modal n times, where n is the number of possible translations.

AlbertoSinigaglia commented 3 years ago

Hi all, I'm using this code:

$prods = Product::whereIn('product_id', function($query) use ($search){
    return $query->fromSub(
        ProductTranslation::search($search, 0)->select('product_id')->distinct(),
        'tmp'
    );
})->get();

where Product is the main model, ProductTranslation is the translation models, and the searchable array looks like this:

protected $searchable = [
    /**
     * Columns and their priority in search results.
     * Columns with higher values are more important.
     * Columns with equal values have equal importance.
     *
     * @var array
     */
    'columns' => [
        'products_translations.product_name' => 10,
        'products_translations.product_description' => 5,
    ],
];

this works, but there is for sure better way to do this