Creating a search function using Laravel 4

I am trying to create a way for users to search all products on a website. When they search for “Burton snowboards,” I just want snowboard with the burton brand to appear in the results. But if they were looking only for burton, then all products with the brand burton should appear.

This is what I tried to write, but does not work for several reasons.

Controller:

public function search(){ $input = Input::all(); $v= Validator::make($input, Product::$rules); if($v->passes()) { $searchTerms = explode(' ', $input); $searchTermBits = array(); foreach ($searchTerms as $term) { $term = trim($term); if (!empty($term)){ $searchTermBits[] = "search LIKE '%$term%'"; } } $result = DB::table('products') ->select('*') ->whereRaw(". implode(' AND ', $searchTermBits) . ") ->get(); return View::make('layouts/search', compact('result')); } return Redirect::route('/'); } 

I am trying to recreate the first solution given for this problem https://stackoverflow.com/a/4648772

The first problem I identified is that I am trying to blow $input , but it is already an array. So I'm not sure how to do this. And as I wrote ->whereRaw(". implode(' AND ', $searchTermBits) . ") , I am sure that this is not true. I am not sure how to fix these problems, but any ideas or solutions would be much appreciated.

+6
source share
4 answers

You will need to get the conditions from your input field and skip all of them when building the database query. You will also need to set the table field in which you want to search for conditions, in this example the table field name . Here's an untested example, but you get this idea.

 public function search() { $q = Input::get('myInputField'); $searchTerms = explode(' ', $q); $query = DB::table('products'); foreach($searchTerms as $term) { $query->where('name', 'LIKE', '%'. $term .'%'); } $results = $query->get(); } 
+19
source

I made a plugin for laravel to do it! Hope you use it and give feedback. You can specify relevance for each column and use information from other tables.

https://github.com/nicolaslopezj/searchable

You can search as follows

 $products = Product::search($query)->get(); 
+6
source

Controller Function 1

  public function search() { $q = Input::get('searchKeyWords'); $users = DB::table('users'); $results = $users->where('name', 'LIKE', '%'. $q .'%') ->orWhere('email', 'LIKE', '%'. $q .'%') ->orWhere('password', 'LIKE', '%'. $q .'%') ->get(); return View::make('users.search')->with('users', $results); } 

Controller Function 2

  public function search() { $q = Input::get('searchKeyWords'); $results = User::where('name', 'LIKE', '%'. $q .'%') ->orWhere('email', 'LIKE', '%'. $q .'%') ->orWhere('password', 'LIKE', '%'. $q .'%') ->get(); return View::make('users.search')->with('users', $results); } 

Controller Function 3

 public function search() { $results = User::where('name', 'LIKE', '%'. Input::get('searchKeyWords') .'%') ->orWhere('email', 'LIKE', '%'. Input::get('searchKeyWords') .'%') ->orWhere('password', 'LIKE', '%'. Input::get('searchKeyWords') .'%') ->get(); return View::make('users.search')->with('users', $results); } 
+2
source

This is an example when I try to use Eloquent with many search terms.

 public function search() { $q = Input::get('myInputField'); $searchTerms = explode(' ', $q); $query = $article->where('title', 'LIKE', "%{$keyword}%") ->orWhere('description', 'LIKE', "%{$keyword}%"); foreach($searchTerms as $term) { $query = $query->orWhere('name', 'LIKE', '%'. $term .'%'); } $results = $query->get(); } 

You can also change the function ->get() to other results, such as ->paginate() , and it should be like $results = $query->paginate(10);

+1
source

Source: https://habr.com/ru/post/956802/


All Articles