调用Illuminate\Pagination\Paginator::make()报错:未定义该方法
Fixing "Call to undefined method Illuminate\Pagination\Paginator::make()" Error
Hey there, that error pops up because the Paginator::make() method was removed in Laravel 5.3 and later versions. Instead, you'll need to use the LengthAwarePaginator class to manually paginate your collection. Here's how to adjust your code:
Step 1: Update Your Import
Replace the Paginator import with LengthAwarePaginator:
use Illuminate\Support\Collection; use Illuminate\Support\Facades\Input; use Illuminate\Pagination\LengthAwarePaginator; // This is the replacement
Step 2: Rewrite the Pagination Logic
Here's the revised code that works with modern Laravel versions:
$collection = new Collection($guarantee_ticket); $perPage = 3; // Use the framework's built-in method to get the current page (more reliable than manual Input get) $currentPage = LengthAwarePaginator::resolveCurrentPage(); // Calculate the starting index for slicing $offset = ($currentPage - 1) * $perPage; $pagedData = $collection->slice($offset, $perPage)->all(); // Create the paginator instance $pagination = new LengthAwarePaginator( $pagedData, count($collection), // Total number of items $perPage, // Items per page $currentPage, // Current page number [ 'path' => LengthAwarePaginator::resolveCurrentPath(), // Ensures correct pagination URLs ] );
Key Notes:
LengthAwarePaginatoris designed for scenarios where you know the total number of items (exactly what your original code was doing withPaginator::make()).- Using
resolveCurrentPage()andresolveCurrentPath()ensures your pagination works seamlessly with Laravel's request handling, avoiding issues with manual input parsing. - The
pathparameter is crucial—it tells the paginator what base URL to use for generating pagination links (e.g.,url.com/testin your example).
内容的提问来源于stack exchange,提问作者Martin AJ
相关产品推荐
相关产品推荐

