Category Archives: Laravel

Pagination for Laravel 4.1 | Working with Laravel Custom Pagination | Laravel 4.1 Pagination

Working with Laravel 4.1 pagination was not so difficult though the resource in internet is low rather than the Laravel 4.0. Up to day Laravel 4.1 has some inbuilt problem (which they are making correction) had given me some hard time, like with the static function. Anyway I recovered them and lets talk about how we can manage the custom pagination with Laravel 4.1.

We will be using “paginate” method with Eloquent model or the Query Builder. We will be using Laravels “Paginator” class which has a “make” method which allow us to create the custom pagination. Oh, one thing, if you guys are using simple raw mysql queries its the high time that you should change it to a ORM. I will discuss the usefulness of ORM on another post, but, briefly it allows you to change the db frequently, gives you more handle to relational database, gives you more power to accumulate things shortly as they have lots of method to help you out to save your raw coding of sql’s. For example you want to find all the data of the table, and its very simple like $table->;find(), almost every ORM has method like this. Eloquent is one of the easiest ORM I have seen so far. Lets come to the point how we can do it icon smile Pagination for Laravel 4.1 | Working with Laravel Custom Pagination | Laravel 4.1 Pagination

In my case I am giving example of a contact table where all table information should in paginated form. To achieve this we will create a new method in my Contact repository. The code sample is like bellow.

/**
* Getting the contacts as paginated
*
* Get results by page
*
* @param int $page
* @param int $limit
* @return StdClass
*/
public function getByPage($page = 1, $limit = 10){
    $results = new StdClass;
    $results->page = $page;
    $results->limit = $limit;
    $results->totalItems = 0;
    $results->items = array();
    $contacts = Contact::skip($limit * ($page - 1))
                           ->take($limit)
                           ->get();  
    $totalCont = Contact::all()->count();
    $results->totalItems = $totalCont;
    $results->items = $contacts->all();  
    return $results;
}

Now in your controller call the getByPage method and make the pagination. The code should be as follows

/**
* Function to list contacts
* @author Zea
* @version 0.1
* @copyright iVive Labs
* @access pulic
* @param optional pagination-> page, limit
* @return contacts
*/
public function listContact(){
    $page = Input::get('page', 1);
    $limit = Input::get('limit', 10);
    $contact = new Contact;  // correct
    $data = $contact->getByPage($page, $limit);
    $contacts = Paginator::make($data->items, $data->totalItems, $limit);      

    foreach ($contacts->getCollection() as $key => $contact){
        $explode_contact[$key]= array (
            'id' => $contact->id,
            'name' => $contact->name,
            'street' => $contact->street,
            'zipcode' => $contact->zipcode,
            'city' => $contact->city
        );         
    }      
    $explode_contact['paging'] = (array("page"=>$contacts->getCurrentPage(),"pageSize" =>$contacts->count(), "pageCount"=>ceil($contacts->getTotal()/$contacts->getPerPage()),"total"=>$contacts->getTotal()));
       
    $explode_contact['success'] = true;
    return json_encode($explode_contact);
}

I guess it will solve the pagination things with Laravel 4.1

The Paginator class has lots of methods for creating the pagination. Some useful ones are as follows:

find
count
getTo
getFrom
getTotal
getPerPage
getLastPage
getCurrentPage

There are more methods over there, you can check it by this link.