Laravel:搜索或过滤集合

时间:2014-08-01 11:52:01

标签: php laravel laravel-4 eloquent

我在过滤或搜索集合时遇到此问题

http://laravel.io/bin/vj115检查网址代码。

我想要做的是通过get方法过滤集合(来自url ofcourse) 但只有当Input::get('category')具有价值时才有效,否则无效。

请您查看代码并告诉我需要修复的内容?

感谢。

=====真实代码只是因为链接在将来被破坏(编辑)=============

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

感谢Dave ..我解决了它:

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

2 个答案:

答案 0 :(得分:7)

我怀疑它与你如何使用Eloquent有关。如果对象是使用" new"创建的,则不能简单地将方法应用于对象。关键字。

$newBrands = new Brand;

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

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

如果您使用方法静态创建它,它将起作用。

$newBrands = Brand::limit(100);

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

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

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

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

答案 1 :(得分:0)

在这里,我正在根据显示名或全名或电子邮件搜索用户名。因此,$request->filled('name of your input')是解决方案。

 $usernames = (new User())->newQuery(); //where User is the model
        if($request->filled('email')){
            $usernames->orWhere('email',$request->email);
        }
        if($request->filled('full_name')){
            $usernames->orWhere('full_name',$request->full_name);
        } if($request->filled('display_name')){
            $usernames->orWhere('display_name',$request->display_name);
        }
 $usernames = $usernames->pluck('username')->toArray();