skip to Main Content

I want to show pagination on my page based on the data from the resource collection.

I have done this code for get data in collection and paginate.

 return auth()->user()->hasRole('admin')
            ? ArticleResource::collection(Article::latest()->paginate(5))
            : ArticleResource::collection(auth()->user()->articles()->latest()->paginate(5));

2

Answers


  1. First, create a collection class that will extend the bascollection class

     <?php
    
    namespace AppSupport;
    
    use IlluminatePaginationLengthAwarePaginator;
    use IlluminateSupportCollection as BaseCollection;
    
    class Collection extends BaseCollection
    {
        public function paginate($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,
            ]
        );
    }
    

    }

    Than you can use in controller like this

     return auth()->user()->hasRole('admin')
            ? new Collection(ArticleResource::collection(Article::latest())->paginate(5);
            : new Collection(ArticleResource::collection(auth()->user()->articles()->latest())->paginate(5);
    

    l

    Login or Signup to reply.
  2. this is how i am using

    public function render()
    {
        $paginate = Config::get('core.paginate');
        $portals = Portal::where(function ($query) {
            if (auth()->user()->hasRole('Endorsement')) {
                //Filter the portal for endorsement  role
                $query->whereJsonContains('spoc', (string)auth()->id());
            }
            if (auth()->user()->hasRole('CLCM')) {
                //Filter the portal for CLCM  role
                $query->whereJsonContains('clcm', (string)auth()->id());
            }
        })->where(function ($query) {
            $query->where('title', 'like', '%' . $this->search . '%')
                ->orWhere('id', 'like', '%' . $this->search . '%')
                ->orWhere('policynumber', 'like', '%' . $this->search . '%')
                ->orWhere('startdate', 'like', '%' . $this->search . '%')
                ->orWhere('enddate', 'like', '%' . $this->search . '%')
                ->orWhere('opentoenroll', 'like', '%' . $this->search . '%')
                ->orWhere('isenabled', 'like', '%' . $this->search . '%');
        })->where('isenabled', 'yes');
        if ($this->sortField == 'remaining_days'){
            //Remaining day's column is not in our table that's why we are using this approach
            $portals = (new Collection($portals->get()->sortBy('remaining_days')))->paginate($paginate);
        }else{
          $portals =  $portals->orderBy($this->sortField, $this->sortAsc ? 'asc' : 'desc')->paginate($paginate);
        }
        return view('clcm::default.dashboard.livewire', compact('portals'));
    }
    

    In the blade file, you just have to do this

    {{$portal->links()}}

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search