Crazylife's avatar

Call to a member function paginate() on array error message

This is my controller.

 $default= DB::table('products')
             ........
              ->paginate(5)->toArray();

    $notDefault= DB::table('products')
               .......
                ->paginate(5)->toArray();

    $lists = array_merge($default, $notDefault);

 $currentPage = LengthAwarePaginator::resolveCurrentPage();
      // Create a new Laravel collection from the array data

      $col = new Collection($lists);
      // Define how many items we want to be visible in each page
      $perPage = 5;
      // Slice the collection to get the items to display in current page
      $currentPageSearchResults = $col->slice(($currentPage - 1) * $perPage, $perPage)->all();
      // Create our paginator and pass it to the view
      $paginatedItems= new LengthAwarePaginator($currentPageSearchResults , count($col), $perPage);
      // set url path for generted links
      $paginatedItems->setPath($request->url());
return View::make('listing',['lists' => $paginatedItems],compact('id_p'));

This is my route.

Route::get('listing/{id}', ['uses' => 'Controller@listing', 'as' => 'prod_listing']);

This is my view

 @foreach($lists as $list)
........
                @endforeach
        {{$lists ->link()}}

I tried this but get "Trying to get property of non-object" error. Anyone can tell me what is the problem? How can i do pagination with array? Thanks.

0 likes
4 replies
Crazylife's avatar

I solved my problem by just removing the paginate() in db table. Can i know how to change the next and previous button from arrow to string?

Crazylife's avatar

I am using Illuminate\Pagination\LengthAwarePaginator to do it, maybe this cannot be applied?

1 like

Please or to participate in this conversation.