mardi 26 décembre 2017

Laravel match multiple search words

I am trying to modify a template that uses Laravel and I am very new to this language. I am trying to filter search results to match ALL searched terms.

For example, user searches for "Red", search returns all red results. User searches for "Trucks", search returns all trucks. But if a user searches for "Red Trucks", search returns both "red" search results and "trucks" search results. So red cars and red bikes are there, as well as yellow trucks etc... But I only want "Red Trucks" to be shown. If a result doesn't have one of the tags saved with it, I don't want it shown. Currently it just has to contain one of the tags and it's shown.

I know I need to modify the "where" and "orwhere" parts for the 'tags' and probably change them to '=', but I can't get it to work through my trial and error.

For reference, an uploaded photo of a red truck would have the following in the tag field in phpmyadmin: red,lowered,s10,truck,gloss,custom,stripes

So searching for "Red Truck"should return this image, and not include any "red" "cars".

Base Code:

public function search($search, $timeframe = null)
{
$extends = explode(' ', $search);
$images = $this->posts($timeframe)->Where('tags', 'LIKE', '%' . $search . '%')
    ->whereNull('deleted_at')->whereNotNull('approved_at')->orderBy('approved_at', 'desc');

foreach ($extends as $extend) {
        $images->orWhere('tags', 'LIKE', '%' . $extend . '%')->whereNotNull('approved_at')->whereNull('deleted_at');
}

return $images = $images->with('user', 'comments', 'favorites')->whereNotNull('approved_at')->whereNull('deleted_at')->paginate(perPage());

}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2pHdYNB
via IFTTT

Aucun commentaire:

Enregistrer un commentaire