Hi, I am trying to paginate all these elements, but whatever I tried it shows all on one page, it doesnt split this on more pages.
public function referralTransactions($id){
$user_id=$id;
$refs=User::where('sponsor_id', '=', $user_id)->pluck('id');
$alltxns=[];
$i=0;
//$m=0;
if(count($refs)>0) {
foreach ($refs as $key =>$referral) {
$alltxn = $this->buyselltransactionlist($referral);
//$paginate = $this->getpaginateforbuysell($referral, 5);
//$m=$m count($alltxn);
$alltxns[$i]=$alltxn;
$i ;
}
}
dd($alltxns);
return view('adminpartials.referral_transactions',
['user_id'=>$user_id,
// 'lists'=>$lists
'alltxns'=>$alltxns,
//'referrals'=>$referrals,
'refs'=>$refs,
]
);```
CodePudding user response:
in controller use paginate(number you want perPage), example :
#Controller
$posts=Post::latest()->paginate(number you want)
return view('index')->with('posts', $posts);
in View
#loop items
{{ $posts->links() }}
CodePudding user response:
This can be achieved by using a Collection
macro.
<?php
namespace App\Providers;
use Illuminate\Support\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
/**
* Paginate a standard Laravel Collection.
*
* @param int $perPage
* @param int $total
* @param int $page
* @param string $pageName
* @return array
*/
Collection::macro('paginate', function($perPage, $total = null, $page = null, $pageName = 'page') {
$page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);
return new LengthAwarePaginator(
$this->forPage($page, $perPage),
$total ?: $this->count(),
$perPage,
$page,
[
'path' => LengthAwarePaginator::resolveCurrentPath(),
'pageName' => $pageName,
]
);
});
}
}
Then you use like any other collection helper.
collect([...])->paginate(15);
Ref: https://gist.github.com/simonhamp/549e8821946e2c40a617c85d2cf5af5e
CodePudding user response:
public function paginate($items, $perPage = 15, $page = null, $options = [])
{
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
$items = $items instanceof Collection ? $items : Collection::make($items);
return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
}
$items = $this->paginate($collection);