Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating search functionality with Laravel 4

Tags:

php

mysql

laravel

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.

like image 547
Mitch Avatar asked Oct 26 '13 21:10

Mitch


3 Answers

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();

}
like image 93
afarazit Avatar answered Sep 21 '22 00:09

afarazit


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();
like image 45
Nicolás López Avatar answered Sep 23 '22 00:09

Nicolás López


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);
}
like image 38
Qamar Uzman Avatar answered Sep 22 '22 00:09

Qamar Uzman