How to Search Laravel Model?

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

First of all, we will edit our controller as follows.

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

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

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

Route::get('/search/', '[email protected]')->name('search');

Now all we have to do is open a search.blade.php file in our view file, we open a form as follows

<form action="{{url('/search/')}}"><br class="page_speed_1240208953"=""> <input type="text" name="q" placeholder="Search..." required><br class="page_speed_1240208953"=""></form>

Now we're all set to make a call. From now on, all you have to do is show our data.

I hope this article was useful.


KARABAY A, 2020 . How to Search Laravel Model ?,

(Accessed June 14, 2020).

  Share this post

Comments (0)


Sign up for the email newsletter to be the first to know about my blog posts