Paginate Method Example in Laravel 8

Websolutionstuff | Jun-28-2021 | Categories : Laravel

In this post i will share you information about paginate method example in laravel 8.

As we know laravel provide many paginate method for custom pagination in laravel 8, Here, i will give you information about laravel paginate function, and we will see how to use pagination in laravel with example.

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

 

$paginator->count()    

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

"count:7" // no of count

 

$paginator->total()

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

 

$paginator->setPageName($name)

It will help you to set 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 get the query string variable used to store the page. and also get the page which is set by using setPageName() function.

"page"

 

$paginator->firstItem()

Using this method we can get the result 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 true or false value. if you have more pages then it will return true otherwise get 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 value 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 result number of the last item in the results. in your data getting last no record using this fuction.

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 total no of records in currenct page with it's details in 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 current page is 1 then previous url get 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"

 

Recommended Post
Featured Post
Laravel 9 MongoDB CRUD Operation Tutorial
Laravel 9 MongoDB CRUD Operati...

In this article, we will see the laravel 9 MongoDB crud operation tutorial. In laravel, we will have many crud operation...

Read More

Dec-06-2022

How To Get Current Week Records In MySQL
How To Get Current Week Record...

In this tutorial, we will see how to get current week records in MySQL. Many times we need to get current week reco...

Read More

Feb-07-2022

Laravel AJAX CRUD example
Laravel AJAX CRUD example

Today I will show you how to create ajax crud operations in laravel. In laravel 6/7 ajax crud operation, we can perform...

Read More

May-14-2020

Laravel 9 User Role and Permission
Laravel 9 User Role and Permis...

In this article, we will show you laravel 9 user role and permission with an example. here we will see how to set u...

Read More

Mar-02-2022