laravel分页重定向到索引页

时间:2019-06-07 21:11:15

标签: php laravel pagination

我有这样的json

{#232 ▼
  +"current_page": 1
  +"data": array:2 [▼
    0 => {#230 ▼
      +"id": 1
      +"test_col": "Test one"
    }
    1 => {#237 ▼
      +"id": 3
      +"test_col": "Test three"
    }
  ]
  +"first_page_url": "http://api/api/test?page=1"
  +"from": 1
  +"last_page": 8
  +"last_page_url": "http://api/api/test?page=8"
  +"next_page_url": "http://api/api/test?page=2"
  +"path": "http://api/api/test"
  +"per_page": 2
  +"prev_page_url": null
  +"to": 2
  +"total": 15
}

为了得到分页,我正在做这些代码

$response =  $response->getBody()->getContents();
$outputs = (json_decode($response));
 $outputs = (json_decode($response));
        $pagination = new LengthAwarePaginator($outputs->data, $outputs->total, $outputs->per_page, $outputs->current_page);
 return view('test.index')->with('outputs',$outputs)->with('pagination',$pagination);

现在,当我来到前端时,我可以看到这些 enter image description here

但是当我单击第2页时,我将重定向到我的索引页,当我这样做时 dd($pagination->links()); 我看到了这个结果

<ul class="pagination" role="navigation">
    <li class="page-item disabled" aria-disabled="true" aria-label="&laquo; Previous">
        <span class="page-link" aria-hidden="true">&lsaquo;</span>
    </li>

    <li class="page-item active" aria-current="page"><span class="page-link">1</span></li>
    <li class="page-item"><a class="page-link" href="/?page=2">2</a></li>
    <li class="page-item"><a class="page-link" href="/?page=3">3</a></li>
    <li class="page-item"><a class="page-link" href="/?page=4">4</a></li>


    <li class="page-item">
        <a class="page-link" href="/?page=2" rel="next" aria-label="Next &raquo;">&rsaquo;</a>
    </li>
</ul>

1 个答案:

答案 0 :(得分:0)

Laravel带有默认的pagination系统。

通过控制器获取数据,例如

$perPage = 10;
$data = MyModel::paginate($perPage);
return view('test.index', compact('data'));

然后在刀片视图中显示它:

<div class="container">
    @foreach ($data $d)
        {{-- Display the data here --}}   
    @endforeach
</div>

分页链接将自动添加

{{ $data-links() }}