Laravel 8 Pagination Example

In this article, we will see laravel 8 pagination example. As we know laravel provide many paginate method for custom pagination in laravel 8, Here, we will see information about the laravel paginate() function and how to use pagination in laravel 8 with example.

So, let's see pagination example in laravel 8, custom pagination in laravel 8, how to use pagination in laravel 8, laravel 8 pagination bootstrap, laravel 8 pagination, paginate laravel 8, laravel paginate collection, laravel pagination example blade, pagination in laravel 7/8/9.

Using this method you can simply customize your pagination in laravel 8, below paginator instance provides additional pagination information.

$paginator->count()    

Using this function you can get the number of items on the current page.

"count:7" // no of count

 

 

$paginator->total()

This function is used for the total number of matching items in the data store. It is not available when we use the simplePaginate() function in laravel.

 

$paginator->setPageName($name)

It will help you to set the query string variable used to store the page.

Illuminate\Pagination\Paginator {#1290 ▼
  #hasMore: true
  #items: Illuminate\Database\Eloquent\Collection {#1288 ▶}
  #perPage: 7
  #currentPage: 1
  #path: "http://localhost:8000"
  #query: []
  #fragment: null
  #pageName: "page"
  +onEachSide: 3
  #options: array:2 [▼
    "path" => "http://localhost:8000"
    "pageName" => "page"
  ]
}

 

$paginator->getPageName()

This function gets the query string variable used to store the page. And also get the page that is set by using the setPageName() function.

"page"

 

$paginator->firstItem()

Using this method we can get the resulting number of the first item in the results.

1 //get first item

 

$paginator->hasMorePages()

It will help you to check if there are more items in the data store. In this function, you will get a true or false value. if you have more pages then it will return true otherwise get a false value.

true // get boolean value

 

 

$paginator->getUrlRange($start, $end)

This method create a range of pagination URLs.

array:5 [▼
  1 => "http://localhost:8000?page=1"
  2 => "http://localhost:8000?page=2"
  3 => "http://localhost:8000?page=3"
  4 => "http://localhost:8000?page=4"
  5 => "http://localhost:8000?page=5"
]

 

$paginator->currentPage()

You can get the current page number. In this function, you will get current page values like 1,2, etc.  

2 // get current page value 

 

$paginator->hasPages()

You can check if there are enough items to split into multiple pages.

true // get boolean value

 

$paginator->lastItem()

Get the resulting number of the last item in the results. In your data getting last no record using this function.

7 // get last record

 

 

$paginator->getOptions()

Get the paginator options.

array:2 [▼
  "path" => "http://localhost:8000"
  "pageName" => "page"
]

 

$paginator->items()

Get the items for the current page. items() function return details of your current page like the total no of records on the current page with its details in the array.

array:7 [▼
  0 => App\Models\Post {#1300 ▼
    #table: "posts"
    #guarded: []
    #connection: "mysql"
    #primaryKey: "id"
    #keyType: "int"
    +incrementing: true
    #with: []
    #withCount: []
    #perPage: 15
    +exists: true
    +wasRecentlyCreated: false
    #attributes: array:14 [▶]
    #original: array:14 [▶]
    #changes: []
    #casts: []
    #classCastCache: []
    #dates: []
    #dateFormat: null
    #appends: []
    #dispatchesEvents: []
    #observables: []
    #relations: []
    #touches: []
    +timestamps: true
    #hidden: []
    #visible: []
    #fillable: []
  }
  1 => App\Models\Post {#1301 ▶}
  2 => App\Models\Post {#1302 ▶}
  3 => App\Models\Post {#1303 ▶}
  4 => App\Models\Post {#1304 ▶}
  5 => App\Models\Post {#1305 ▶}
  6 => App\Models\Post {#1306 ▶}
]

 

$paginator->nextPageUrl()

 Using this method you can get the URL for the next page.

"http://localhost:8000?page=2"

 

 

$paginator->previousPageUrl()

Get the URL for the previous page. if the current page is 1 then the previous URL gets a null value.

"http://localhost:8000?page=1"

 

$paginator->perPage()

The number of items to be shown per page.

7 // total no of record in per page

 

$paginator->lastPage()

Get the page number of the last available page. (Not available when using simplePaginate).

 

$paginator->onFirstPage()

Determine if the paginator is on the first page.

false // return boolean value

 

 

$paginator->url($page)

Get the URL for a given page number.

"http://localhost:8000?page=1"

 


You might also like :

RECOMMENDED POSTS

FEATURE POSTS