【问题标题】:Can't paginate data in Laravel无法在 Laravel 中对数据进行分页
【发布时间】:2020-02-21 17:33:17
【问题描述】:

我已成功检索数据并将其显示在表格列表中,但我的列表没有分页

我尝试每页显示 15 个数据,但它在页面中一次显示所有数据。我的数据来自 API。

我的控制器

 public function index()
    {


        $response =  $this->client->get('getUserIndex')->getBody();
        $content = json_decode($response->getContents(),true);

        $collection = new \Illuminate\Support\Collection($content);

        $total = count($collection['data']);

        $paginationRecord = CollectionPaginate::paginate($collection, $total, 15);


         return view('configuration.comuserprofiles.ComUserProfilesList', compact('paginationRecord'));
}

我的表格视图的一半

   @foreach($paginationRecord as $i=>$user)

                            @foreach($user as $i => $u)

                             @php

                                $currentRecordno = 1;
                           // dd($u['irel__com_access_level']['ID']);
                            @endphp  

                            <tr>


                                <td>{{ $currentRecordno + $i }}</td>
                                 <td>{{ $u['ID'] }}</td>
                             <td>{{ $u['NICK_NAME'] }}</td>
                             <td>{{ $u['PWD_INVALID_HIT'] }}</td>

                         </tr>
                         @endforeach

我的分页刀片

<div class="row row-pagination">
    <div class="col-sm-6 text-left">
        <small class="text-muted inline m-t-sm m-b-sm"><font color="#00000f">{{ $paginationRecord->total() }} record/s found </font></small> 
    </div>
    <div class="col-md-6 text-right text-center-xs">
        <ul>
            {{ $paginationRecord->links() }}
        </ul>
    </div>
</div>  

我希望每页显示 15 条记录

【问题讨论】:

    标签: php json laravel pagination


    【解决方案1】:

    使用take()Collection的方法获取特定数量的元素

    $paginationRecord = CollectionPaginate::paginate($collection->take(15), $total, 15);
    

    您也可以使用forPage()

    $page = Illuminate\Pagination\Paginator::resolveCurrentPage() ?: 1;
    $paginationRecord = new Illuminate\Pagination\LengthAwarePaginator($collection->forPage($page, 15), $total, 15, $page);
    

    【讨论】:

    • @adaminho 哪一个没用,你得到了什么?
    【解决方案2】:

    已经通过在控制器的 $content 中添加 ['data'] 解决了

    $content = $content['data'];
    
    
    

    【讨论】:

      猜你喜欢
      • 2021-06-14
      • 2018-04-06
      • 2017-09-13
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 2018-10-01
      • 2019-04-24
      • 1970-01-01
      相关资源
      最近更新 更多