title: Creating search functionality with Laravel 4
body: <p>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. </p>
  
  <p>This is what I have attempted to write but isn't working for multiple reasons. </p>
  
  <p>Controller:</p>
  
  <pre><code> public function search(){
      $input = Input::all();
  
      $v= Validator::make($input, Product::$rules);
  
      if($v-&gt;passes())
      {
  
          $searchTerms = explode(' ', $input);
          $searchTermBits = array();
          foreach ($searchTerms as $term) {
              $term = trim($term);
              if (!empty($term)){
                  $searchTermBits[] = "search LIKE '%$term%'";
              }
          }
          $result = DB::table('products')
          -&gt;select('*')
          -&gt;whereRaw(". implode(' AND ', $searchTermBits) . ")
          -&gt;get();
  
          return View::make('layouts/search', compact('result')); 
      }
      return Redirect::route('/');    
  }
  </code></pre>
  
  <p>I am trying to recreate the first solution given for this <a href="https://stackoverflow.com/questions/3621310/php-sql-select-where-like-search-item-with-multiple-words">stackoverflow.com problem</a></p>
  
  <p>The first problem I have identified is that i'm trying to explode the <code>$input</code>, but it's already an array. So i'm not sure how to go about fixing that. And the way I have written the <code>-&gt;whereRaw(". implode(' AND ', $searchTermBits) . ")</code>, i'm sure isn't correct. I'm not sure how to fix these problems though, any insights or solutions will be greatly appreciated. </p>