Nurdaulet Shamilov
Nurdaulet Shamilov

Reputation: 119

Like Function not working for the searching

I am trying to create search function but showing nothing from search

public function search(Request $request) 
{
    $search = $request->get('search');
    $dictionaries = Dictionary::all()->where('title', 'LIKE', "%{$search}%");

    return view('dictionary', compact('dictionaries'));
}

Upvotes: 0

Views: 54

Answers (1)

Tim Lewis
Tim Lewis

Reputation: 29314

You can't call ::all() before ->where() in this manner. You need to restructure your query:

$dictionaries = Dictionary::where('title', 'LIKE', "%{$search}%")->get();

When you call ::all(), you're converting your Dictionary query to a Collection, and the ->where() method on a Collection is different than that of the Builder class.

Upvotes: 5

Related Questions