Level 44
You'll have to use something like this in order to paginate on a collection:
https://gist.github.com/simonhamp/549e8821946e2c40a617c85d2cf5af5e
If you do that you can get rid of your first paginate and just use get()->map( ...
I want to use laravel pagination on map and i returned array collection my code is below
$categories = Category::with(['children'=>function($query){
$query->select('name','parent_id');
}],'parent')->paginate(10)->map(function($item){
$category = collect($item->children)->map(function($chil_category) {
return $chil_category->name;
})->toArray();
return (object) [
'id'=>$item->id,
'name'=>$item->name,
'parent'=> (isset($item->parent->name) && !empty($item->parent->name) ? $item->parent->name : 'N/A'),
'children'=> (count($category) > 0 ? substr(implode(' , ',$category),0,50) : 'N/A'),
'slug'=>$item->slug,
];
});
and my result is
Collection {#230 ▼
#items: array:2 [▼
0 => array:5 [▼
"id" => 1
"name" => "First Category"
"parent" => "N/A"
"children" => "Child of First Category"
"slug" => "first-category"
]
1 => array:5 [▼
"id" => 2
"name" => "Child of First Category"
"parent" => "First Category"
"children" => "N/A"
"slug" => "child-of-first-category"
]
]
}
so how can i use laravel pagination on it and how it possible on mapping?
Please or to participate in this conversation.