簡體   English   中英

Laravel:搜索或過濾集合

[英]Laravel : search or filter the collection

我在過濾或搜索集合時遇到了這個問題

http://laravel.io/bin/vj115檢查代碼的 url。

我想要做的是通過 get 方法過濾一個集合(當然來自 url)但只有當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
    ));
}

這就是我的設計的樣子

感謝戴夫..我解決了它:

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

我懷疑這與您使用 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);

在這里,我正在根據顯示名稱或全名或電子郵件搜索用戶名。 因此, $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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM