Laravel Pagination Not Working While Using Map Method [solved]

  • 09-07-2022
  • 1535
  • Laravel 9
  • Haresh Chauhan

Laravel Pagination Not Working While Using Map Collection, This issue you will face while you need to modify and add some extra data inside the collection and taking help of the map collection method or loop in your collection.

So when will you use map() the collection or loop To modify data inside the collection you will lose your pagination. Especially while you working on API development.

There are many solutions for it, But in this post, I will tell you the best solution for collecting data with modification without lost pagination.

First, I have taken the Issue/Problem and then I have given solution below the example.

Pagination Issue With Map Collection

Here, User, I am calling 10 records per page, and also I want to add some extra static attributes for all users to know their age and date of birth. This two attribute not available in the data users table. still, I want to add these two attributes for all users.

For that, I have taken the help of the map collection method, But while I am listing that user the pagination is no more available, Because I have a user map function.

So the map method will not return you users' pagination it just gives you a collection of user data. You can see in the below example the pagination is no more available.

$user = User::paginate(10)->map(function($item){
    return [
        'age' => 22,
        'dob' => '16-06-1999',
        'name' => $item->name,
        'email' => $item->email,
    ];
});

dd($user);

This is the output but the pagination is no more available even though i used the pagination method.

Illuminate\Support\Collection {#266 ▼
    #items: array:10 [▼
        0 => array:4 [▼
        "age" => 22
        "dob" => "16-06-1999"
        "name" => "MIFtQFBjt3"
        "email" => "cjxuEaqXIX@gmail.com"
        ]
        1 => array:4 [▶]
        2 => array:4 [▶]
        3 => array:4 [▶]
        4 => array:4 [▶]
        5 => array:4 [▶]
        6 => array:4 [▶]
        7 => array:4 [▶]
        8 => array:4 [▶]
        9 => array:4 [▶]
    ]
}

It just returns array collection without pagination.

How To Fix Pagination

Stop using map collection method, Instead of it you can use getCollection() This will return our user's data without losing pagination.

I just collect 10 user data per page and then transform that collection using getCollectio() method.

$user = User::paginate(10);

$user->getCollection()->transform(function($item){

    $item->age = 22;
    $item->dob = '16-06-1999';
    $item->name = $item->name;
    $item->email = $item->email;

    // Insted you call here db table

    // $item->role_name = DB::table('roles')->where('id',$item->role_id)->value('role_name');
    
    return $item;
});

dd($user);

Here is the output with the user data returning with the pagination.

Illuminate\Pagination\LengthAwarePaginator {#987 ▼
    #total: 50506
    #lastPage: 5051
    #items: Illuminate\Database\Eloquent\Collection {#1201 ▼
        #items: array:10 [▼
        0 => App\User {#1202 ▶}
        1 => App\User {#1203 ▶}
        2 => App\User {#1204 ▶}
        3 => App\User {#1205 ▶}
        4 => App\User {#1206 ▶}
        5 => App\User {#1207 ▶}
        6 => App\User {#1208 ▶}
        7 => App\User {#1209 ▶}
        8 => App\User {#1210 ▶}
        9 => App\User {#1211 ▶}
        ]
    }
    #perPage: 10
    #currentPage: 1
    #path: "http://post.dv"
    #query: []
    #fragment: null
    #pageName: "page"
    +onEachSide: 3
    #options: array:2 [▶]
}

So this is how you can fix the pagination issue using getCollection() method.


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :