Skip to content

Instantly share code, notes, and snippets.

@rubanraj54
Forked from Repox/PaginationController.php
Created April 2, 2019 16:37
Show Gist options
  • Save rubanraj54/e0acee89fb2e528b5b82fd5a2b6b9afb to your computer and use it in GitHub Desktop.
Save rubanraj54/e0acee89fb2e528b5b82fd5a2b6b9afb to your computer and use it in GitHub Desktop.
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment