我想在刀片中合并两个模型。看看我的代码
public function history_posts_requests () {
$reports = Report::latest()->where('status', '=', '1')->first()->get();
$order_mobiles = OrderMobile::latest()->where('status', '=', '1')->first()->get();
$allOrders = $reports->merge($order_mobiles)->paginate(50);
return view('Admin.desktops.history_posts_requests', compact('allOrders'));
}
但是我得到这个错误。
方法Illuminate \ Database \ Eloquent \ Collection :: paginate不存在。
答案 0 :(得分:3)
我建议您使用Paginator
和LengthAwarePaginator
类来实现分页:
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
public function history_posts_requests () {
$reports = Report::latest()->where('status', '=', '1')->first()->get();
$order_mobiles = OrderMobile::latest()->where('status', '=', '1')->first()->get();
$allOrders = $reports->merge($order_mobiles);
$totalGroup = count($allOrders);
$perPage = 10;
$page = Paginator::resolveCurrentPage('page');
$allOrders = new LengthAwarePaginator($allOrders->forPage($page, $perPage), $totalGroup, $perPage, $page, [
'path' => Paginator::resolveCurrentPath(),
'pageName' => 'page',
]);
return view('Admin.desktops.history_posts_requests', compact('allOrders'));
}