Search function Laravel 5.8 add detail to search logic

Posted 1 month ago by callumcarlstrom


I currently have a search function in my application that works just fine but I want it to return a broader search. Let me explain.

I have a recipe application where each user can upload a recipe. In the search bar, you can search for recipes and that returns a result based on what is in the database. My problem is, if you search for something in plural, or misspell a little bit, there is no output. Example: search for smoothies (plural) when the recipes are titled smoothie (singular). However, if you search for a recipe and the word is incomplete (smooth, smo, sm) then it returns all the results that have the completed word. So I want that same functionality, but for words that have too many letters or are in plural.

Here is my current logic:

public function search(Request $request){
        $q = $request->input('search');
        $recipes = Recipe::where('title', 'LIKE', '%'.$q.'%')->orWhere('description', 'LIKE', '%'.$q.'%')->get();
        return view('recipes.results')->with('recipes', $recipes)->with('query', $q);

The function searches through titles and descriptions of recipes.

I appreciate any help I can get!

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.