对于流明,我有一个问题,即这一直是1
,当我转到/artikel?page=2
时:
LengthAwarePaginator::resolveCurrentPage();
完整的代码:
<?php namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
class ArtikelController extends Controller {
public function index()
{
$dir = '../resources/views/artikel/';
$files = array_diff(scandir($dir), array('..', '.'));
$artikel = array();
foreach($files as $k => $v)
{
$id = substr($v,0,1);
$artikel[$id]['id'] = $id;
$artikel[$id]['name'] = substr($v,0,strpos($v,'.blade.php'));
}
//Get current page form url e.g. &page=6
$currentPage = LengthAwarePaginator::resolveCurrentPage();
#dd($currentPage);
//Create a new Laravel collection from the array data
$collection = new Collection($artikel);
//Define how many items we want to be visible in each page
$perPage = 2;
//Slice the collection to get the items to display in current page
$currentPageResults = $collection->slice($currentPage * $perPage, $perPage)->sortByDesc('id')->all();
//Create our paginator and pass it to the view
$paginatedResults = new LengthAwarePaginator($currentPageResults, count($collection), $perPage);
$paginatedResults->setPath('artikel');
return view('artikel', ['artikel' => $paginatedResults]);
}
我找不到错误。可能是什么原因? (我还更新到"laravel/lumen-framework": "5.1.*"
)
答案 0 :(得分:0)
您可以使用这种简单的方式获取当前页面:
$currentPage = (int) app('request')->get('page', $default = '0');