我尝试像下面这样手动创建分页实例
use Illuminate\Pagination\LengthAwarePaginator as Paginator;
public function createPaginate()
{
$page =1; // Get the current page or default to 1
$perPage = 2;
//this is my array
$items = $this->getCurrentServices($this->getServices(new MaintenanceService, 'maintenance_service')->get());
$offset = ($page * $perPage) - $perPage;
$maintenanceServices = new Paginator (
array_slice($items, $offset, $perPage, true),
count($items),$perPage,Paginator::resolveCurrentPage(),
array('path' => Paginator::resolveCurrentPath())
);
return view('my-view', compact('maintenanceServices'));
上面的代码在my-view
中运行良好,但在页面之间切换时数据没有改变
我还尝试使用 Request
更改我的代码以检查网址
$maintenanceServices =new Paginator (
array_slice($items, $offset, $perPage, true),
count($items), $perPage, $page,
['path' => $request->url(), 'query' => $request->query()]);
最后我使用dd($items)
检查了$items
,结果是
array:7 [▼
0 => MaintenanceService {#245 ▶}
1 => MaintenanceService {#246 ▶}
2 => MaintenanceService {#247 ▶}
3 => MaintenanceService {#248 ▶}
4 => MaintenanceService {#249 ▶}
5 => MaintenanceService {#250 ▶}
6 => MaintenanceService {#251 ▶}
]
有什么建议吗?
最佳答案
在 Laravel 5.1 中你可以执行以下操作
$pageStart = request()->get('page', 1);
$offset = ($pageStart * $perPage) - $perPage;
关于php - 在 Laravel 5.1 中手动创建分页器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34242121/