Reputation:
I want to merge two models in a blade. Look at my codes
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'));
}
But I get this error.
Method Illuminate\Database\Eloquent\Collection::paginate does not exist.
Upvotes: 0
Views: 6711
Reputation: 1954
I suggest you to use Paginator
and LengthAwarePaginator
classes to achieve the pagination:
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'));
}
Upvotes: 3