My Blog Posts

How to Search on laravel model ?

How to Search on laravel model ?


Hi friends in this blog post laravel will also learn how to make the search through the model.

First, we'll organize our controller as follows.

public function search(Request $request){
$blog = Blog::query()
->where('title', 'LIKE', "%{$searchTerm}%")
->orWhere('content', 'LIKE', "%{$searchTerm}%")
->get();
return view('search', compact('blog'));
}

This returns all data with a title or content that contains the string.

After setting up our controller as above, we also edit our web.php file as follows

Route::get('/search/', 'HomeController@search')-name('search');

Now we're just opening a search.blade.php file in our view file, and we're opening a form as follows.



We've got everything ready to make a call now. From now on, all you need to do is show our data.

I hope this post has been useful.



Share this article


Comments (0)

Comment