Related content

Laravel provides several ways to paginate items like pagination with the numeric numbers or next with the previous button or sometimes even more like custom pagination. But no pagination for laravel collection. If you want to create pagination with laravel collection, then you have to make it your own.

We can easily show pagination links by using the paginate method on the query builder or an Eloquent query. Remember that, the argument passed to the paginate helper function is the number of items we want to display "per page".

So in this example, I will simply show you how to create laravel pagination resource collection. If you don't know laravel pagination get collection, then this tutorial will help you to create laravel resource collection pagination. So let's see the example of laravel paginate array.

Preview of Laravel Collection Pagination


Step 1: Install Laravel

First of all, we need to get a fresh Laravel 9 version application using the bellow command, So open your terminal OR command prompt and run the bellow command to start laravel collection pagination:

composer create-project laravel/laravel example-app


Step 2: Connect Database

After successfully installing the laravel app and then configuring the database setup. We will open the ".env" file and change the database name, username and password in the env file to create laravel resource collection pagination.




Read also: Laravel 9 Pagination Example With Bootstrap


Step 3: Create Migration and Model

In this step, we need to create users table and model. then we need to run a migration. so let's change that on both files.



use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
     * Run the migrations.
     * @return void
    public function up()
        Schema::create('users', function (Blueprint $table) {

     * Reverse the migrations.
     * @return void
    public function down()


Now update the user model by replacing it with the below code:


namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
    use HasFactory, Notifiable;
     * The attributes that are mass assignable.
     * @var array
    protected $fillable = [
        'name', 'email', 'password'
     * The attributes that should be hidden for arrays.
     * @var array
    protected $hidden = [
        'password', 'remember_token',
     * The attributes that should be cast to native types.
     * @var array
    protected $casts = [
        'email_verified_at' => 'datetime',


Now you have to run this migration by following the command:

php artisan migrate


Read also: Laravel 9 Pagination With Previous And Next Button


Step 4: Create Route

Here, we need to add one more route to display the users data with collection pagination. so let's add that route in the web.php file.



use Illuminate\Support\Facades\Route;
use App\Http\Controllers\TutorialController;

| Web Routes
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
Route::get('/', [TutorialController::class,'index']);


Step 5: Create Controller

Here, we need to add the index() method for fetching users data with collection pagination in laravel in TutorialController. so let's add like as below:



namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use App\Utils\PaginateCollection;

class TutorialController extends Controller
    public function index()
        $users = User::get();
        $users = PaginateCollection::paginate($users, 4);
        return view('welcome',compact('users'));


Step 6: Create Helper Class

In this step, we need to create a custom helper class to create pagination for laravel collection. So update it like:



namespace App\Utils;

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

class PaginateCollection
    public static function paginate(Collection $results, $showPerPage)
        $pageNumber = Paginator::resolveCurrentPage('page');
        $totalPageNumber = $results->count();

        return self::paginator($results->forPage($pageNumber, $showPerPage), $totalPageNumber, $showPerPage, $pageNumber, [
            'path' => Paginator::resolveCurrentPath(),
            'pageName' => 'page',


     * Create a new length-aware paginator instance.
     * @param  \Illuminate\Support\Collection  $items
     * @param  int  $total
     * @param  int  $perPage
     * @param  int  $currentPage
     * @param  array  $options
     * @return \Illuminate\Pagination\LengthAwarePaginator
    protected static function paginator($items, $total, $perPage, $currentPage, $options)
        return Container::getInstance()->makeWith(LengthAwarePaginator::class, compact(
            'items', 'total', 'perPage', 'currentPage', 'options'


Step 7: Create Blade file

In this step, we need to create a new master blade file and update for user blade file. so let's change it.


<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <title>{{ config('', 'Laravel') }}</title>
    <!-- Tailwindcss -->
    <script src=""></script>

    <!-- Scripts -->
    @vite(['resources/sass/app.scss', 'resources/js/app.js'])
    <div id="app">
        <nav class="navbar navbar-expand-md navbar-light bg-white shadow-sm">
            <div class="container">
                <a class="navbar-brand" href="{{ url('/') }}">
                <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="{{ __('Toggle navigation') }}">
                    <span class="navbar-toggler-icon"></span>

                <div class="collapse navbar-collapse" id="navbarSupportedContent">
                    <!-- Left Side Of Navbar -->
                    <ul class="navbar-nav me-auto">


                    <!-- Right Side Of Navbar -->
                    <ul class="navbar-nav ms-auto">
                        <!-- Authentication Links -->
                            @if (Route::has('login'))
                                <li class="nav-item">
                                    <a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a>

                            @if (Route::has('register'))
                                <li class="nav-item">
                                    <a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a>
                            <li class="nav-item dropdown">
                                <a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
                                    {{ Auth::user()->name }}

                                <div class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
                                    <a class="dropdown-item" href="{{ route('logout') }}"
                                        {{ __('Logout') }}

                                    <form id="logout-form" action="{{ route('logout') }}" method="POST" class="d-none">

        <main class="py-4">


Now create a welcome blade file and update it like this: 



<style type="text/css">
    .my-active span{
        background-color: #5cb85c !important;
        color: white !important;
        border-color: #5cb85c !important;
    ul.pager>li {
        display: inline-flex;
        padding: 5px;
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Laravel 9.x Pagination Tutorial | Laravelia</div>
                 <div class="card-body">
                    <table style="width: 100%">
                            @foreach($users as $user)
                                <td>{{ $loop->index + 1 }}</td>
                                <td>{{ $user->name }}</td>
                                <td>{{ $user->email }}</td>
                    <center class="mt-5">
                        {{ $users->links() }}


Look at that, to display the pagination links, we used the links() method and using this links() method, we can display laravel pagination resource collection.


Read also: Laravel 9 Custom Pagination Example


Ok, now we are ready to go and test how to show pagination links in laravel tutorial. So let's run the project using this command:

php artisan serve


Now you can test our application by visiting the below URL:




Now we know laravel collection paginate does not exist. Hope this laravel pagination custom collection tutorial will help you to create laravel pagination resource collection.