Deep dive into Laravel Pagination — PHP IteratorAggregate

Source: internet

Regarding to Laravel pagination doc, when calling the paginate method, you will receive an instance of Illuminate\Pagination\LengthAwarePaginator. When calling the simplePaginate method, you will receive an instance of Illuminate\Pagination\Paginator. These objects provide several methods that describe the result set. In addition to these helpers methods, the paginator instances are iterators and may be looped as an array.

There is a basic example of Laravel pagination in your project

public function index(Request $request)
{
$users = User::paginate(15);
return view('user.index', ['users' => $users]);
}
<div class="container"> @foreach ($users as $user) {{ $user->name }} @endforeach</div>

Here is what you see in detail of $users variable

Now let look at inside LengthAwarePaginator class, we can see that it implements several interfaces such as ArrayAccess, Countable, IteratorAggregate, JsonSerializable which are known as predefined interfaces in PHP. This class also extends AbstractPaginator which has getIterator() to return ArrayIterator

In this case, we consider the IteratorAggregate is an interface to create an external Iterator which allows you to traverse your custom class objects using foreach. With the Interface synopsis, IteratorAggregate extends Traversable interface itself is an abstract base interface (with no methods as shown in the interface synopsis) that cannot be instantiated. However, it can be used to check whether a class is traversable using foreach or not.

class Food implements IteratorAggregate
{
public array $arr; public function __construct()
{
$this->arr = ['Milk', 'Cake', 'Coke'];
}
public function getIterator()
{
return new ArrayIterator($this->arr);
}
}$obj = new Food();foreach($obj as $key => $value)
{
echo $key ." =>" . $value . "\n";
}
======================================OUTPUT0 => 'Milk'1 => 'Cake'2 => 'Coke'

IteratorAggregate is an easy way to implement an Iterator. The advantage with IteratorAggregate is the speed, it is much faster than its alternatives. The problem with it it’s that… despite the name it’s not an iterator but a traversable (so again, no next, key, current, valid, rewind methods).

Once you stop learning you start dying (4S: Search — Summarize — Share — Smile)

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to learn C++?

The Biggest Problem that Flutter is Facing!

Keeping the Daggers Sharp ⚔️

Meet our Open Leaders 7 Grads

AWS Fundamentals: Section 6- API Gateway & Lambda

Slack for monitoring your Laravel-App

Image result for laravel + slack image

How can we decide what is the best ERP system for our business?

Literally, stuck in the past

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ngo Dinh Cuong

Ngo Dinh Cuong

Once you stop learning you start dying (4S: Search — Summarize — Share — Smile)

More from Medium

Laravel - Send an asynchronous request with Guzzle

Laravel whereHas and orWhereHas Query Example

Laravel Advance | Laravel Model Caching -Performance Boost Tutorial

How to use UUID in Laravel for primary keys?

user Migration with uuid