Laravel : search or filter the collection

2019-07-23 15:44发布

I have this issue while filtering or searching through a collection

http://laravel.io/bin/vj115 check the url for code.

What i am trying to do is filter a collection by get method (from url ofcourse) But only it only works when Input::get('category') has value else nothing works.

Could you please check the code and let me know what need to be fixed?

Thanks.

===== Real Code just incase the link is broken in future (edited)=============

public function anyIndex() {
    $id = Input::get('id');
    $brand = Brand::firstOrNew(array('id' => $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->brands();
       $appends['category'] = Input::get('category');
    }

    if (Input::get('status')) {
       $status = Input::get('status') == 'published' ? 1 : 0;
          $newBrands->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->orderBy($order_by, $order);
    }

    $brands = $newBrands->paginate($paginate);
    $brand_categories_list = new BrandCategory;
    $selected_cats = array();

    if ($id != "") {
       $selected_cats = $brand->categories->lists('id');
    }

    return View::make('admin.brands.index')
    ->with(array(
        'selected_cats' => $selected_cats,
        'brand' => $brand,
        'brands' => $brands,
        'brand_categories_list' => $brand_categories_list->lists('name', 'id'),
        'appends' => $appends,
        'i' => $i
    ));
}

This is how my design looks

Thanks to Dave.. I solved it as :

    public function anyIndex() {
    $id = Input::get('id');
    $brand = Brand::firstOrNew(array('id' => $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->brands();
        $appends['category'] = Input::get('category');
    } else {
        $newBrands = Brand::limit(-1);
    }
    if (Input::has('status')) {
        $status = Input::get('status') == 'published' ? 1 : 0;
        $newBrands->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->orderBy($order_by, $order);
    }else{
        $newBrands->orderBy('name', 'asc');
    }


    $brands = $newBrands->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->categories->lists('id');
    }
    return View::make('admin.brands.index')
                    ->with(
                            array(
                                'selected_cats' => $selected_cats,
                                'brand' => $brand,
                                'brands' => $brands,
                                'brand_categories_list' => $brand_categories_list->lists('name', 'id'),
                                'appends' => $appends,
                                'i' => $i)
    );
}

1条回答
看我几分像从前
2楼-- · 2019-07-23 16:10

I suspect it has to do with how you are using Eloquent. You can't simply apply methods to the object if it was created using the "new" keyword.

$newBrands = new Brand;

// This won't work
$newBrands->where('is_active', '=', $status);

// This will work
$newBrands = $newBrands->where('is_active', '=', $status);

It will work if you create it statically along with a method.

$newBrands = Brand::limit(100);

// This will work
$newBrands->where('is_active', '=', $status);

Fluent (DB) works the same way.

$newBrands = DB::table('brands');

// This wil work
$newBrands->where('is_active', '=', $status);
查看更多
登录 后发表回答