Selfmade LengthAwarePaginator returns items not in an array and thus they can't be accessed using axios.get

Published 1 year ago by hillcow

When using paginate() the items are in an array. When using my selfmade LengthAwarePaginator following the guide below they are not. For some reason the axios.get request returns all data but the items (maybe it can't access collections?).

What can I do?

1 year ago (252,680 XP)

following the guide below

What guide?


@hillcow Also, why are you hand-rolling a length-aware paginator? What’s wrong with Laravel’s?

1 year ago (14,560 XP)

@jlrdw my bad, here it is:

@martinbean: sorry, bad phrasing. I am using the build in paginator, I just meant that I manually used it instead of just using ->paginate().

here is the difference of the results:


manual paginator:

To be honest I am not aware of why axios can't get a collection.


@hillcow The first thing I see is that data is an object and not an array.

How are you instantiating this LengthAwarePaginator instance? Why are you instantiating it manually instead of via an Eloquent model or query builder instance?

1 year ago (14,560 XP)

@martinbean in both cases? because on paginate it has these: [] And I guess this is why I dont get any items via axios.

I just followed this guide to create the LengthAwarePaginator instance:

Because in my case I think I cant use paginate():


@hillcow So you’re fetching the latest 20 threads, sorted by votes? Why does that need to be paginated?

1 year ago (252,680 XP)

Also even a lengthaware should return same type results as normal laravel pagination.

1 year ago (14,560 XP)

@martinbean this is just an example, I will fetch a loooot more.

@jlrdw: as you can see on the pictures in my second post this doesn't seem to be the case?

1 year ago (252,680 XP)

My laware

public function ownerlist()
        if (isset($_REQUEST['t1'])) {
            $t1 = $_REQUEST['t1'];
        } else {
            $t1 = "";

        if (isset($_REQUEST['page'])) {
            $page = $_REQUEST['page'];
        } else {
            $page = "1";
        $perpage = "5";
        $offset = ($page - 1) * $perpage;

        $ownersearch = $t1;
        $ownersearch = $ownersearch . "%";
        $krows = \Illuminate\Support\Facades\DB::select('select COUNT(ownerid) as count from powners where oname like :oname', ["oname" => $ownersearch]);
        $numrows = $krows[0]->count;
        echo $numrows;

        $data = DB::table('powners')
                        ->where('oname', 'like', $ownersearch)
                        ->orderBy('oname', 'asc')
        $pagelinks = LengthPager::makeLengthAware($data, $numrows, $perpage, ['t1' => $t1]);
        return view('owner/pownerlist')->with('data', $data)->with('data2', $pagelinks);

the class

<?php namespace App\Services;

use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;

abstract class LengthPager

   * Create paginator
   * @param  Illuminate\Support\Collection  $collection
   * @param  int     $total
   * @param  int     $perPage
   * @return string
  public static function makeLengthAware($collection, $total, $perPage, $appends = null)
  $paginator = new LengthAwarePaginator(
    ['path' => Paginator::resolveCurrentPath()]

  if($appends) $paginator->appends($appends);

  return str_replace('/?', '?', $paginator->render());
  //return str_replace('/?', '?', $paginator);

}//end class

snipplet from view

echo "<td>"."<a>".$owner->oname."</a>"."</td>";
$owner->oname /////a returned object

This was just a quick example I did for another answer, but does work.

Also @bestmomo helped me write this a while back.

1 year ago (297,250 XP)

What do you mean by it can't access collections?

you should be able to access your data by in your javascript closure and just use a foreach statement to loop through it to display it on the page.

Another way to fix this problem is to transform the response from the controller.


@hillcow Why don’t you actually tell us what you’re trying to do instead of how you’re trying to do it?

You should seldom have to manually instantiate a paginator instance for a query. Tell us what you’re trying to do and we’ll be able to help you form a query that’ll return a “proper” paginator.

1 year ago (14,560 XP)


I have a model Thread. I would like to sort the latest 100 threads by a column 'hot'. So I don't want to have a query asking for the whole database, but rather to ask for latest 100 threads (by id) which in turn should be ordered by 'hot'. And I want the result of that to be paginated.


@hillcow Could you post your self made LengthAwarePaginator code!

Please sign in or create an account to participate in this conversation.