Home > Enterprise >  How to show pagination of resource collection to blade file in laravel?
How to show pagination of resource collection to blade file in laravel?

Time:11-11

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));

CodePudding user response:

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

 <?php

namespace App\Support;

use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection 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

CodePudding user response:

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()}}

  • Related