I am trying to create a way for users to search through all the products on a website. When they search for "burton snowboards", I only want the snowboards with the brand burton to appear in the results. But if they searched only "burton", then all products with the brand burton should appear.
This is what I have attempted to write but isn't working for multiple 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 stackoverflow.com problem
The first problem I have identified is that i'm trying to explode the $input
, but it's already an array. So i'm not sure how to go about fixing that. And the way I have written the ->whereRaw(". implode(' AND ', $searchTermBits) . ")
, i'm sure isn't correct. I'm not sure how to fix these problems though, any insights or solutions will be greatly appreciated.
You'll need to get the terms from your input field and loop through all of them while building your DB query. You'll also need to set the table field in which you want the terms to be searched, in this the example the table field is name
. Here's an untested example but you'll get the 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();
}
I made a plugin for laravel to make this! Hope you use it and give feedback. You can specify the relevance for each column and use information from other tables
https://github.com/nicolaslopezj/searchable
You can search like this
$products = Product::search($query)->get();
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);
}
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);
}
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With