title: Laravel : search or filter the collection
body: <p>I have this issue while filtering or searching through a collection </p>
  
  <p><a href="http://laravel.io/bin/vj115" rel="nofollow noreferrer">http://laravel.io/bin/vj115</a> check the url for code. </p>
  
  <p>What i am trying to do is filter a collection by get method (from url ofcourse)
  But only it only works when <code>Input::get('category')</code> has value else nothing works. </p>
  
  <p>Could you please check the code and let me know what need to be fixed?</p>
  
  <p>Thanks.</p>
  
  <p>===== Real Code just incase the link is broken in future (edited)=============</p>
  
  <pre class="lang-php prettyprint-override"><code>public function anyIndex() {
      $id = Input::get('id');
      $brand = Brand::firstOrNew(array('id' =&gt; $id));
      $paginate = Misc::getSettings('admin-pagination');
      $page_no = isset($_GET['page']) ? $_GET['page'] : 1;
      $i = ($paginate * $page_no) - ($paginate - 1);
      $appends = false;
      $newBrands = new Brand;
  
      if (Input::get('category')) {
         $brandCat = BrandCategory::find(Input::get('category'));
         $newBrands = $brandCat-&gt;brands();
         $appends['category'] = Input::get('category');
      }
  
      if (Input::get('status')) {
         $status = Input::get('status') == 'published' ? 1 : 0;
            $newBrands-&gt;where('is_active', '=', $status);
      $appends['status'] = Input::get('status');
      }
  
      if (Input::get('order_by') || Input::get('order')) {
         if (Input::get('order_by')) {
            $order_by = Input::get('order_by');
            $appends['order_by'] = Input::get('order_by');
         } else {
            $order_by = 'name';
         }
  
      if (Input::get('order')) {
            $order = Input::get('order');
            $appends['order'] = Input::get('order');
         } else {
            $order = 'asc';
         }
  
         $order = Input::get('order') ? Input::get('order') : 'asc';
         $newBrands-&gt;orderBy($order_by, $order);
      }
  
      $brands = $newBrands-&gt;paginate($paginate);
      $brand_categories_list = new BrandCategory;
      $selected_cats = array();
  
      if ($id != "") {
         $selected_cats = $brand-&gt;categories-&gt;lists('id');
      }
  
      return View::make('admin.brands.index')
      -&gt;with(array(
          'selected_cats' =&gt; $selected_cats,
          'brand' =&gt; $brand,
          'brands' =&gt; $brands,
          'brand_categories_list' =&gt; $brand_categories_list-&gt;lists('name', 'id'),
          'appends' =&gt; $appends,
          'i' =&gt; $i
      ));
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/AClOe.png" alt="This is how my design looks"></p>
  
  <p>Thanks to Dave.. I solved it as  :</p>
  
  <pre><code>    public function anyIndex() {
      $id = Input::get('id');
      $brand = Brand::firstOrNew(array('id' =&gt; $id));
      $paginate = Misc::getSettings('admin-pagination');
      $page_no = isset($_GET['page']) ? $_GET['page'] : 1;
      $i = ($paginate * $page_no) - ($paginate - 1);
      $appends = false;
  
  
      if (Input::has('category')) {
          $brandCat = BrandCategory::find(Input::get('category'));
          $newBrands = $brandCat-&gt;brands();
          $appends['category'] = Input::get('category');
      } else {
          $newBrands = Brand::limit(-1);
      }
      if (Input::has('status')) {
          $status = Input::get('status') == 'published' ? 1 : 0;
          $newBrands-&gt;where('is_active', '=', $status);
          $appends['status'] = Input::get('status');
      }
      if (Input::has('order_by') || Input::has('order')) {
          if (Input::has('order_by')) {
              $order_by = Input::get('order_by');
              $appends['order_by'] = Input::get('order_by');
          } else {
              $order_by = 'name';
          }
          if (Input::has('order')) {
              $order = Input::get('order');
              $appends['order'] = Input::get('order');
          } else {
              $order = 'asc';
          }
          $order = Input::get('order') ? Input::get('order') : 'asc';
          $newBrands-&gt;orderBy($order_by, $order);
      }else{
          $newBrands-&gt;orderBy('name', 'asc');
      }
  
  
      $brands = $newBrands-&gt;paginate($paginate);
      /* $queries = DB::getQueryLog();
        $last_query = end($queries);
        dd($last_query); */
      $brand_categories_list = new BrandCategory;
      $selected_cats = array();
      if ($id != "") {
          $selected_cats = $brand-&gt;categories-&gt;lists('id');
      }
      return View::make('admin.brands.index')
                      -&gt;with(
                              array(
                                  'selected_cats' =&gt; $selected_cats,
                                  'brand' =&gt; $brand,
                                  'brands' =&gt; $brands,
                                  'brand_categories_list' =&gt; $brand_categories_list-&gt;lists('name', 'id'),
                                  'appends' =&gt; $appends,
                                  'i' =&gt; $i)
      );
  }
  </code></pre>