Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jinsonjose's avatar

How to apply pagination in different query results ?

How to apply pagination in different query results ?

                                                ->join('sectors', 'sectors.id','incidents.sector_id')
                                                ->join('incident_notes', 'incident_notes.incident_id','incidents.id')
                                                ->leftJoin('bdms', 'bdms.id','sectors.bdm_id')
                                                ->where('bdms.user_id',Auth::user()->id)
                                                ->whereIn('sectors.id',$inactiveSectorIncident)
                                                ->orderBy($field, $sort)
                                                ->limit($countSourceActiveNote)
                                                ->groupby('sectors.id')
                                                ->get();



                        $sourceInActiveNote = Sector::select('sectors.id', 'sectors.sector_type', 'sectors.name', 'sectors.created_at',DB::raw('IFNULL( sectors.deleted_at, "In-Active") as type'),DB::raw('MAX(sector_notes.updated_at) as activity_date'),DB::raw('IFNULL( sectors.deleted_at, "Source Note") as activity_type'),DB::raw('IFNULL( sectors.country, "UK") as country'))
                                                    ->leftJoin('bdms', 'bdms.id','sectors.bdm_id')
                                                    ->leftJoin('sector_notes', 'sector_notes.sector_id','sectors.id')
                                                    ->whereIn('sectors.id',$inactiveSectorNote)
                                                    ->where('bdms.user_id',Auth::user()->id)
                                                    ->orderBy($field, $sort)
                                                    ->limit($countSourceInActiveIncident)
                                                    ->groupby('sectors.id')
                                                    ->get();

$result = $sourceActiveIncident->merge($sourceActiveNote);```

//how to apply pagination  on result array in laravel any idea

//like this

->skip(($page - 1) * $countSourceActiveIncident)
->limit($countSourceActiveIncident)
0 likes
2 replies
MichalOravec's avatar

Add this to boot() method of AppServiceProvider and then you can paginate on a collection.

use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        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
            ]);
        });
    }
}

Please or to participate in this conversation.