alexhiggins's avatar

Laravel 5 Paginator chunk error

I know Taylor completely rewritten the paginator class so there might be a few things a miss with it potentially. However, has anyone come across this error?

Call to undefined method Illuminate\Pagination\Paginator::chunk()

This worked a few days ago when I last updated it. (... just to skip unnecessary stuff)

@foreach($recipes->chunk(3) as $recipeChunk)
  ...
    @foreach($recipeChunk as $recipe)
     ...
    @endforeach
...
@endforeach
0 likes
9 replies
garygreen's avatar

This is because in the old paginator any functions that weren't defined in the paginator class it would defer them onto the collection. The chunk function is actually part of Illuminate\Support\Collection -- not sure why Taylor has removed the dynamic call functionality, possibly just forgotten to add it back in.

isimmons's avatar

Just playing around I added this back to Paginator.php

/**
  * Call a method on the underlying Collection
  *
  * @param string $method
  * @param array $arguments
  * @return mixed
  */
 public function __call($method, $arguments)
 {
  return call_user_func_array(array($this->getCollection(), $method), $arguments);
 }

 /**
  * Get a collection instance containing the items.
  *
  * @return \Illuminate\Support\Collection
  */
 public function getCollection()
 {
  return new Collection($this->items);
 }

which gets it using the Collection::chunk() method again. Now I found the links() method is also missing. Might have to just wait for docs or try to figure out the new paginator by code.

isimmons's avatar

@jeffreyWay is there another way of doing this or do you know if the above methods to refer back to the collection method will be put back in?

Valorin's avatar

On this topic, does anyone know how to change the renderer so it provides the page number selector the L4 one uses?

isimmons's avatar

I made a helper method that seems to be working correctly. It took me a while because of over thinking things and trying to figure out how to mimic what the Collection::chunk() method is doing.

helpers.php loaded via AppServiceProvider:

//chunks the underlying collection in a paginator instance
if(! function_exists('p_chunk'))
{
 function p_chunk($paginator, $size, $preserveKeys = false)
 {
  $items = [];

  foreach ($paginator as $key => $value)
  {
   $items[] = $value;
  }

  $collection = new \Illuminate\Support\Collection($items);

  return $collection->chunk($size, $preserveKeys);
 }
}

Called like so in the view

@foreach(p_chunk($users, 4) as $userSet)
         <div class="row users">
            @foreach($userSet as $user)
                <div class="col-md-3 user-block">
                    @include('users.partials.avatar', ['size' => 70])
                    <h4 class="user-block-username">
                        {{{ link_to_route('users.profile', $user->username, [$user->username]) }}}
                    </h4>
                 </div>
            @endforeach
        </div>
    @endforeach

Please let me know if you find any problems with it

1 like
isimmons's avatar

@Valorin yeah I still haven't figured that out. Looking in the default presenter (BootstrapThreePresenter) it looks as if it should be getting prevButton/links/nextButton but it's only showing the prev and next buttons for me.

Maybe Taylor is not finished with paginator or things will become more clear when the docs are updated.

Valorin's avatar

@isimmons, yeah, I figure he's so focused on Laravel being easy to use that he will eventually get the documentation written and it will all make sense. I'm just impatient :-)

Please or to participate in this conversation.