How to make a custom pagination from an array in laravel.

Shail Gandhi
3 min readOct 17, 2020

--

Hello all,

Here I am come up with new feature of the laravel, So let get started…

We must have aware about the array, so some times we need to convert that array in the laravel pagination, we can do it easily in just few steps.

Step 1 : We need to create one trait file inside the app directory

<?phpnamespace App\Traits;use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
trait PaginationTrait {public function paginate($items, $perPage = 5, $page = null)
{
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
$total = count($items);
$currentpage = $page;
$offset = ($currentpage * $perPage) - $perPage ;
$itemstoshow = array_slice($items , $offset , $perPage);
return new LengthAwarePaginator($itemstoshow ,$total ,$perPage);
}
}

** Please take a note : The purpose of making a trait is for reusability.
** File location : app/Traits/PaginationTraits.php is more preferable.

Step 2: Import this trait into the file where you want to create pagination from a array. Here I am importing this trait in home controller.

Like this:

<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;
use App\Traits\PaginationTrait;
class HomeController extends Controller
{
use PaginationTrait;
.....
.....
}

In above code I imported the pagination trait in the home controller.

Step 3 : Just pass your array as an argument in paginate function which u have created in PaginationTrait.

Just like this.

<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;
use App\Traits\PaginationTrait;
class HomeController extends Controller
{
use PaginationTrait;

public function index()
{
$users = [
['name' => 'U1', 'designation' => 'Developer'],
['name' => 'U2', 'designation' => 'Developer'],
['name' => 'U3', 'designation' => 'Developer'],
['name' => 'U4', 'designation' => 'Developer'],
['name' => 'U5', 'designation' => 'Developer'],
['name' => 'U6', 'designation' => 'Developer'],
['name' => 'U7', 'designation' => 'Developer'],
['name' => 'U8', 'designation' => 'Developer'],
['name' => 'U9', 'designation' => 'Developer'],
['name' => 'U10', 'designation' => 'Developer'],
['name' => 'U11', 'designation' => 'Developer'],
['name' => 'U12', 'designation' => 'Developer']
];

$users = $this->paginate($users);

// here you can set your pagination path/route.
$users->withPath('');

return view('welcome', ['users' => $users]);
}
}

** Please take a note : You can pass the number of data which you have to get per page and you can also pass the page number also .

For example :

$users = $this->paginate($users, 6); // 6 data per page.
$users = $this->paginate($users, 6, 2); // page 2 is displayed.

By default I had set 5 data per page. and finally it will give you output like this.

In above example I did not passed any arguments so it will set 5 data automatically
By clicking on 2 it will gives you data of page 2.

Suppose you want to start the pagination directly from 2nd page the you have to just pass a number in paginate function.

In above example I had passed $this->paginate($users, 5 , 2); so I directly get data of page 2.

Conclusion

Laravel is just amazing it provide us full functionality with customization, after reading the above article now you can make a custom pagination same as laravel’s paginate function by making one trait and passing some few arguments.

Trait’s source code :

<?phpnamespace App\Traits;use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
trait PaginationTrait {public function paginate($items, $perPage = 5, $page = null)
{
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
$total = count($items);
$currentpage = $page;
$offset = ($currentpage * $perPage) - $perPage ;
$itemstoshow = array_slice($items , $offset , $perPage);
return new LengthAwarePaginator($itemstoshow ,$total ,$perPage);
}
}

Controller’s source code :

<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;
use App\Traits\PaginationTrait;
class HomeController extends Controller
{
use PaginationTrait;
public function index()
{
$users = [
['name' => 'U1', 'designation' => 'Developer'],
['name' => 'U2', 'designation' => 'Developer'],
['name' => 'U3', 'designation' => 'Developer'],
['name' => 'U4', 'designation' => 'Developer'],
['name' => 'U5', 'designation' => 'Developer'],
['name' => 'U6', 'designation' => 'Developer'],
['name' => 'U7', 'designation' => 'Developer'],
['name' => 'U8', 'designation' => 'Developer'],
['name' => 'U9', 'designation' => 'Developer'],
['name' => 'U10', 'designation' => 'Developer'],
['name' => 'U11', 'designation' => 'Developer'],
['name' => 'U12', 'designation' => 'Developer']
];

$users = $this->paginate($users);
$users->withPath('');
return view('welcome', ['users' => $users]);
}
}

--

--