Abdallah Sakre
Abdallah Sakre

Reputation: 915

How to use pagination on an array of arrays in Laravel

I added the following arrays into all_updates[] as follows :

$all_updates[] = $all_update_in;
$all_updates[] = $all_update_out;
$all_updates[] = $all_update_oth;

The $all_updates output :

array(3) { [0]=> array(0) { } [1]=> array(0) { } [2]=> array(3) { [0]=> object(stdClass)#365 (21) { ["id"]=> int(84) ["created_at"]=> string(19) "2019-10-31 12:24:28" ["updated_at"]=> string(19) "2019-10-31 12:24:28" ["title"]=> string(14) "the tag newest" ["body"]=> string(20) "the tag newest ANSWER" } } }

I'm trying to paginate the final array as follows :

$Final_array= $all_updates->paginate(15);

I'm receiving the following error :

Call to a member function paginate() on a non-object

Upvotes: 0

Views: 2814

Answers (3)

Pravin Prajapati
Pravin Prajapati

Reputation: 50

paginate() function is query builder that's why you can't use here .

but you can do with take() and slice().

like

$skipRow=0;

$data=[
  [
    'id'=>1,
    'name'=>'suresh'
  ]
  [
    'id'=>2,
    'name'=>'mahesh'
  ]
];

$collectedData=collect($data);

$rowData=$collectedData->slice($skipRow)->take(15);

Upvotes: 1

MEDZ
MEDZ

Reputation: 2295

First you want to convert the array to collection using collect($arr)

Then check hakob93 answer on the following thread to move forward: https://laracasts.com/discuss/channels/laravel/how-to-paginate-laravel-collection?page=1#reply=363252

Upvotes: 1

pr1nc3
pr1nc3

Reputation: 8348

You can not paginate an array. You paginate the output of the query which is stored in an array but not the array itself.

That means that your 3 nested arrays should already be paginated before inserted in the parent array.

After that you loop around them and you get the links field. This is your pagination info.

Upvotes: 1

Related Questions