There is a tree of categories (collections). For brevity, I used toArray()

 array(8) { ["id"]=> int(1) ["title"]=> string(3) "SSW" ["link"]=> string(3) "SSW" ["parent"]=> int(0) ["user_id"]=> int(1) ["created_at"]=> string(19) "2019-03-26 08:53:44" ["updated_at"]=> string(19) "2019-03-26 08:53:44" ["children"]=> array(2) { [0]=> array(8) { ["id"]=> int(2) ["title"]=> string(24) "Пользователи" ["link"]=> string(12) "Polyzovateli" ["parent"]=> int(1) ["user_id"]=> int(1) ["created_at"]=> string(19) "2019-03-26 12:07:08" ["updated_at"]=> string(19) "2019-03-27 07:56:57" ["children"]=> array(0) { } } [1]=> array(8) { ["id"]=> int(3) ["title"]=> string(5) "Linux" ["link"]=> string(5) "Linux" ["parent"]=> int(1) ["user_id"]=> int(1) ["created_at"]=> string(19) "2019-03-27 07:25:01" ["updated_at"]=> string(19) "2019-03-27 07:44:38" ["children"]=> array(1) { [0]=> array(8) { ["id"]=> int(4) ["title"]=> string(24) "Пользователи" ["link"]=> string(12) "Polyzovateli" ["parent"]=> int(3) ["user_id"]=> int(1) ["created_at"]=> string(19) "2019-03-27 07:48:27" ["updated_at"]=> string(19) "2019-03-27 07:48:27" ["children"]=> array(0) { } } } } } } 

Is there a way to use all Laravel functions to get all category IDs?

  • And what of this category? You need only the id of what in children ? - Yaroslav Molchan
  • @YaroslavMolchan is all categories. I need all the ID. - Evgeny Nikolaev

1 answer 1

 $ids = $categories->pluck('id')->merge($categories->children->pluck('id'))->toArray(); 
  • Thank. I went to understand. - Evgeny Nikolaev