Pagination::make()
方法在Pagination类中不再存在。
是否有解决方法在Laravel 5中进行手动分页?
答案 0 :(得分:28)
您需要添加用途:
use Illuminate\Pagination\LengthAwarePaginator as Paginator;
现在你可以使用:
$paginator = new Paginator($items, $count, $limit, $page, [
'path' => $this->request->url(),
'query' => $this->request->query(),
]);
以与模型对象上的分页相同的格式获取数据;
答案 1 :(得分:2)
您可以像这样创建手动分页
$data = DB::table('post')->skip(0)->take(20)->get();
答案 2 :(得分:2)
实例化这个类的好方法
use Illuminate\Pagination\LengthAwarePaginator as Paginator;
//...
$paginator = new Paginator($items->forPage($page, $limit), $count, $limit, $page, [
'path' => Paginator::resolveCurrentPath()
]);
注意 items
必须是Collection
对象。
答案 3 :(得分:0)
尝试下面的手动分页代码
<?php
namespace App\Http\Controllers;
use Illuminate\Pagination\LengthAwarePaginator as Paginator;
// use Illuminate\Pagination\Paginator;
use Illuminate\Http\Request;
use App\Product;
class MyController extends Controller
{
public function index(Request $request){
$items = Product::all();
$filter_products = []; // Manual filter or your array for pagination
foreach($items as $item){
if($item['id']>40 && $item['id']<50){
array_push($filter_products, $item);
}
}
$count = count($filter_products); // total product for pagination
$page = $request->page; // current page for pagination
// manually slice array of product to display on page
$perPage = 5;
$offset = ($page-1) * $perPage;
$products = array_slice($filter_products, $offset, $perPage);
// your pagination
$products = new Paginator($products, $count, $perPage, $page, ['path' => $request->url(),'query' => $request->query(),]);
// use {{ $products->appends($_GET)->links() }} to dispaly your pagination
return view('index',['products' => $products]);
}
}
答案 4 :(得分:0)
使用Illuminate\Pagination\LengthAwarePaginator
的示例:
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
public function getItems(Request $request)
{
$items = []; // get array/collection data from somewhere
$paginator = $this->getPaginator($request, $items);
// now we can treat $paginator as an array/collection
return view('some-view')->with('items', $paginator);
}
private function getPaginator(Request $request, $items)
{
$total = count($items); // total count of the set, this is necessary so the paginator will know the total pages to display
$page = $request->page ? $request->page : 1; // get current page from the request, first page is null
$perPage = 3; // how many items you want to display per page?
$offset = ($page - 1) * $perPage; // get the offset, how many items need to be "skipped" on this page
$items = array_slice($items, $offset, $perPage); // the array that we actually pass to the paginator is sliced
return new LengthAwarePaginator($items, $total, $perPage, $page, [
'path' => $request->url(),
'query' => $request->query()
]);
}
然后在some-view.blade.php文件中,例如:
@foreach($items as $item)
{{-- --}}
@endforeach
{{ $items->links() }}
请参见https://laravel.com/docs/5.7/pagination#manually-creating-a-paginator
答案 5 :(得分:0)
public function myData($userid)
{
$data = static::get();
$result = [];
if(!empty($data)){
foreach ($data as $key => $value) {
$result[$value->type.'-'.$value->postid][] = $value;
}
}
$paginate = 10;
$page = Input::get('page', 1);
$offSet = ($page * $paginate) - $paginate;
$itemsForCurrentPage = array_slice($result, $offSet, $paginate, true);
$result = new \Illuminate\Pagination\LengthAwarePaginator($itemsForCurrentPage, count($result), $paginate, $page);
$result = $result->toArray();
return $result;
}
答案 6 :(得分:0)
基于@Avishay28,我认为不需要php array_slice。
在 laravel 8 中工作。
我认为就性能而言,这是一种更好的方法,
因为我们没有查询“SELECT * FROM products
”
$items_per_page = 5;
$page_id = (int)$request->query('page') ?? 1;
$model_docs_count = Product::count();
$total_pages = ceil($model_docs_count / $items_per_page);
$model = Product::skip(($page_id - 1) * $items_per_page)->limit($items_per_page)->latest();
// dd($model->toSql()); "SELECT * FROM `products` WHERE ORDER BY `created_at` DESC LIMIT 5 OFFSET 0"
$custom_paginate = new LengthAwarePaginator($model, $total_pages, $items_per_page, $page_id, [
'path' => $request->url(),
'query' => $request->query()
]);
return response()->json([$custom_paginate], 200);
答案 7 :(得分:-5)
使用分页的另一种方式是这样的:
public function index()
{
$posts = DB::table('posts')->paginate(15);
}