lundi 4 février 2019

Laravel search query matching full words only

Hi I am using the following function to return results from a users keyword search:

public function search(Request $request)
    {
        $q = $request->query('q');
        $keywords = explode(" ", $request->query('q'));
        $resources = Resource::where(function ($query) use ($keywords) {
                                    foreach ($keywords as $keyword) {
                                        $query->orWhere('title', 'like', "%{$keyword}%");
                                    }
                                })
                                ->orWhere(function ($query) use ($keywords) {
                                    foreach ($keywords as $keyword) {
                                        $query->orWhere('description', 'like', "%{$keyword}%");
                                    }
                                })
                                ->paginate(20);
        return view('resources.search',compact('resources','q'));
    }

This works relatively ok, but it is returning results that contain part of the keyword. i.e if I search for Bee it is returning results that have 'be' in the title or description

Is there anyway I can only return results that match the full word?



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2S8Q0II
via IFTTT

Aucun commentaire:

Enregistrer un commentaire