我有两个问题:
$posts = Post::all();
$comments = Comment:all();
我后来合并:
$merge = $posts->merge($comments);
$all = $merge->sortByDesc(function($result) {
return $result->created_at;
});
如何对合并结果进行分页?执行$all->paginate(25)
不起作用。
答案 0 :(得分:0)
Laravel 5:
在Laravel 5中,可以通过实例化paginator轻松完成:
$paginator = new Illuminate\Pagination\Paginator($all, $perPage, $currentPage);
答案 1 :(得分:0)
您可以尝试此操作(Version-5x
):
// Add this at the top of your class
use Illuminate\Pagination\LengthAwarePaginator as Paginator;
// Then use following code:
$perPage = 10;
$currentPage = app('request')->get('page') ?: 1; // or $request->get('page') if available
$paginator = new Paginator($all, $all->count(), $perPage, $currentPage);
对于4.2
,请尝试以下语法:
$items = $all->toArray();
$total = count($items);
$perPage = 10;
$paginator = Paginator::make($items, $total, $perPage);
答案 2 :(得分:0)
Laravel 5:
您可以使用spatie/laravel-collection-macros。
$posts = Post::all();
$comments = Comment:all();
return $comments->merge($posts)->paginate();