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}%")
->get();
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_1783601492"=""> <input type="text" name="q" placeholder="Search..." required><br class="page_speed_1783601492"=""></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.

  Quote

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

https://www.karabayyazilim.com/blog/laravel-model-uzerinden-arama-yapma-nasil-yapilir-2020-06-14-235834

(Accessed June 14, 2020).


  Share this post

Comments (0)

Comment

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