Laravel change pagination data

The paginator’s items is a collection. You can grab it and transform the data like so:

$paginator = $this->items()->where('position', '=', null)->paginate(15);
$paginator->getCollection()->transform(function ($value) {
    // Your code here
    return $value;
});

If you are familiar with tap helper here is the snippet that does exact same.

$paginator = tap($this->items()->where('position', '=', null)->paginate(15),function($paginatedInstance){
    return $paginatedInstance->getCollection()->transform(function ($value) {
        return $value;
    });
});

We can’t chain method getCollection to paginator instance because AbstractPaginator will return paginator’s underlying collection. so the paginator instance will be transformed to Collection. So fix that we can use tap helper.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)