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?

jlrdw
jlrdw
1 year ago (238,490 XP)

following the guide below

What guide?

martinbean

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

hillcow
hillcow
1 year ago (14,460 XP)

@jlrdw my bad, here it is: http://psampaz.github.io/custom-data-pagination-with-laravel-5/

@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:

paginate(): https://i.stack.imgur.com/owDiW.png

manual paginator: https://i.stack.imgur.com/eS3SO.png

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

martinbean

@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?

hillcow
hillcow
1 year ago (14,460 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: http://psampaz.github.io/custom-data-pagination-with-laravel-5/

Because in my case I think I cant use paginate(): https://stackoverflow.com/questions/45037481/laravel-eloquent-nested-query-builder

martinbean

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

jlrdw
jlrdw
1 year ago (238,490 XP)

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

hillcow
hillcow
1 year ago (14,460 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?

jlrdw
jlrdw
1 year ago (238,490 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')
                        ->skip($offset)->take($perpage)->get();
        $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(
    $collection, 
    $total, 
    $perPage, 
    Paginator::resolveCurrentPage(), 
    ['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.

Ruffles
Ruffles
1 year ago (294,940 XP)

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

you should be able to access your data by response.data 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.

martinbean

@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.

hillcow
hillcow
1 year ago (14,460 XP)

@martinbean

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.

vkyeswa

@hillcow Could you post your self made LengthAwarePaginator code!

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