yusuf128's avatar

Laravel Paginate on 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?

0 likes
1 reply

Please or to participate in this conversation.