dpauf28808 2014-11-22 18:48
浏览 103
已采纳

从Laravel中的分页结果集中获取不同的行

I have created the following for a product catelog/lister:

public function index($type_id = null) {
    $filters = $sort = array();
    if (isset($type_id)) {
        $filters['type'] = $type_id;
    } else {
        $filters['type'] = Input::get('type');
    }
    $filters['search'] = Input::get('search');
    $filters['brand'] = Input::get('brand');

    $sort['sort'] = Input::get('sort');
    $sort['sortdir'] = Input::get('dir');

    $productsPaginated = $this->fetchProducts($filters, $sort);

    return View::make('products.products', array(
                'productsList' => $productsPaginated
                    )
    );
}

public function fetchProducts($filters, $sorts, $perpage = 2) {
    print_r($filters);
    $Product = Product::query();
    if (!empty($filters['search']))
        $Product->where('name', 'LIKE', '%' . $filters['search'] . '%');
    if (isset($filters['type']))
        $Product->where('type_id', $filters['type']);
    if (isset($filters['brand']))
        $Product->where('brand_id', $filters['brand']);

    if (isset($sorts['sort']))
        $Product->orderBy($sorts['sort'], $sorts['sortdir']);

    $Product = $Product->paginate($perpage);
    return $Product;
}

Which works well so far.

I am now trying to create some filters so a user can further filter the results.

How can I access and determine distinct rows based on a column in:

$productsPaginated = $this->fetchProducts($filters, $sort);

?

  • 写回答

1条回答 默认 最新

  • duannaiying9662 2014-11-22 19:23
    关注

    The groupBy method not only exists on the query builder but also on the collection class. (which will be returned when calling paginate)

    Take a look at the source on github

    So add an argument to your function and use groupBy

    public function fetchProducts($filters, $sorts, $perpage = 2, $groupBy = null) {
    // code omitted for brevity
        $Product = $Product->paginate($perpage);
        if($groupBy){
            $Product = $Product->groupBy($groupBy);
        }
        return $Product;
    }
    

    Update

    Then there's the lists function that works on collections as well as on query builders...

    $Product->lists('column-name');
    

    Update 2

    I was curious so I did some testing and a found something very weird and I have no idea if its a bug or a feature I don't understand

    When calling groupBy the collection returned has actually only one item (index "") and this item contains an array of the "original" items. So to make lists work. I found this workaround

    $Product = $Product->groupBy($groupBy);
    $Product = new Collection($Product[""]); // \Illuminate\Support\Collection
    $Product = $Product->lists('column-name');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?