Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Laravel 5 Pagination with transform
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class PaginationController extends Controller
{
/**
* @param Request $request
* @return mixed
*/
public function index(Request $request)
{
// Results per page
$limit = 50;
// Creates an instance of Illuminate\Pagination\LengthAwarePaginator
$users = User::paginate($limit);
// getCollection() is a mathod available in Illuminate\Pagination\LengthAwarePaginator
// It retrivies the Collection instance the Paginator will iterate over, allowing you to
// use Collection methods. transform() modifies the collection itself.
$users->getCollection()->transform(function($user, $key) {
return [
'name' => $user->name,
'email' => $user->email,
'custom_attribute' => 'custom value',
];
});
return $result;
}
}
@mykeels
Copy link

mykeels commented Jan 5, 2018

Where is the $result variable created?

Did you mean to assign the result of the transformation to the result variable like:

$result = $users->getCollection()->transform(function($user, $key) {
            return [
                'name' => $user->name,
                'email' => $user->email,
                'custom_attribute' => 'custom value',
            ];
        });
return response()->json($result);

Or is $result a special PHP or Laravel variable?

@stevethomas
Copy link

stevethomas commented Dec 6, 2018

I think $result was a typo.

You would want to return $users.

// Creates an instance of Illuminate\Pagination\LengthAwarePaginator
$users = User::paginate(50);

// iterates paginated items and applies a transformation 
$users->getCollection()->transform(function($user) {
    $user->foo = 'bar';
    return $user;
});

return $users;

@ManojKiranAppathurai
Copy link

ManojKiranAppathurai commented Jul 2, 2020

Check this out. I am using tap helper

$users = tap(User::query()
->paginate(100),function($paginatedInstance){
    return $paginatedInstance->getCollection()->transform(function ($value) {
        $value->test = 'test';
        return $value;
    });
});

@Encang-Cutbray
Copy link

Encang-Cutbray commented Mar 30, 2021

big thanks @ManojKiranA

@fahmiegerton
Copy link

fahmiegerton commented Oct 3, 2021

I can't get it work, in my vscode, the getCollection() is undefined method, I tried it too but didn't return anything. Help?

@ezehkingsleyuchenna
Copy link

ezehkingsleyuchenna commented Oct 12, 2021

@fahmiegerton try doing without the getCollection()

@fahmiegerton
Copy link

fahmiegerton commented Oct 12, 2021

@fahmiegerton try doing without the getCollection()

Like this?

$users->transform(function($user, $key) {
            return [
                'name' => $user->name,
                'email' => $user->email,
                'custom_attribute' => 'custom value',
            ];
        });

@untillnesss
Copy link

untillnesss commented Jan 10, 2022

Nice broo @ManojKiranA

@ManojKiranAppathurai
Copy link

ManojKiranAppathurai commented Jan 20, 2022

hope it helps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment