Laravel 根据数组 分页 Laravel 数组 手动分页

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;

use App\Http\Requests;

class ItemsController extends Controller
{
     public function items(Request $request)
    {
        $items = [
            'item1',
            'item2',
            'item3',
            'item4',
            'item5',
            'item6',
            'item7',
            'item8',
            'item9',
            'item10'
            ];

        // Get current page form url e.x. &page=1
        $currentPage = LengthAwarePaginator::resolveCurrentPage();

        // Create a new Laravel collection from the array data
        $itemCollection = collect($items);

        // Define how many items we want to be visible in each page
        $perPage = 1;

        // Slice the collection to get the items to display in current page
        $currentPageItems = $itemCollection->slice(($currentPage * $perPage) - $perPage, $perPage)->all();

        // Create our paginator and pass it to the view
        $paginatedItems= new LengthAwarePaginator($currentPageItems , count($itemCollection), $perPage);

        // set url path for generted links
        $paginatedItems->setPath($request->url());

        return view('items_view', ['items' => $paginatedItems]);
    }

}

view层

 <h1>Items List</h1>

<ul>
@foreach ($items as $item) 
   <li> {{ $item }} </li>
@endforeach
</ul>

<div>
{{ $items->links() }}
</div>

原创文章,作者:星辰,如若转载,请注明出处:http://www.z88j.com/77.html

(9)
打赏 微信扫一扫 微信扫一扫
上一篇 2020年11月17日 上午12:09
下一篇 2020年11月26日 上午1:02

相关推荐

发表回复

登录后才能评论

Warning: error_log(/www/wwwroot/www.z88j.com/wp-content/plugins/spider-analyser/#log/log-1910.txt): failed to open stream: No such file or directory in /www/wwwroot/www.z88j.com/wp-content/plugins/spider-analyser/spider.class.php on line 2900