ShawnZhang31
8/21/2019 - 3:05 PM

[laravel的分页操作]laravel的分页操作 #laravel

[laravel的分页操作]laravel的分页操作 #laravel

1.Model的关系返回的一个Collection

$skinRecordColls=$user->userSkinRecords();

2.ORM在事件区间内的查询

$paginator = $skinRecordColls->whereBetween('created_at',[$star_time,$end_time])
                                     ->latest('created_at')
                                     ->paginate($perPage);

3.laravel的分页返回的是一个对象需要使用laravel的分页对象的内置方法获取里面的数据

foreach($paginator->items() as $item)
        {
            $item_detail;
            switch($item->record_type)
            {
                case "skinrun":
                    $item_detail=SkinRunRecords::where('id',$item->record_id)->first();
                    $detecor=$this->skinRunDetector;
                    break;
            }
            $data[]=$detecor->analysisDetectResult($item, $item_detail, $language);
        } 
$respone = new \stdClass();
$respone->current_page=$paginator->currentPage();
$respone->last_page=$paginator->lastPage();
$respone->data=$data;
$respone->total=$paginator->total();

具体操作详见laravel的文档:https://laravel.com/docs/5.8/pagination#paginator-instance-methods