php Laravel:搜索或过滤集合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25079661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:38:29  来源:igfitidea点击:

Laravel : search or filter the collection

phplaravellaravel-4eloquent

提问by Genius in trouble

I have this issue while filtering or searching through a collection

我在过滤或搜索集合时遇到了这个问题

http://laravel.io/bin/vj115check the url for code.

http://laravel.io/bin/vj115检查代码的 url。

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.

我想要做的是通过 get 方法(来自 url ofcourse)过滤一个集合,但只有它只有在Input::get('category')有价值时才有效,否则什么都不起作用。

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

回答by Dave

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.

我怀疑这与您使用 Eloquent 的方式有关。如果对象是使用“new”关键字创建的,则不能简单地将方法应用于该对象。

$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.

Fluent (DB) 的工作方式相同。

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

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