Advertisement
rodro1

nested category laravel

Oct 13th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. public static function getCategories(){
  2. $get_categories = [];
  3. $categories = \App\BlogCategory::all();
  4. foreach ($categories as $category) {
  5. if($category->parent_id == null){// parent category
  6. $get_categories[$category->id] = $category;
  7. } else {
  8. $get_categories[$category->parent_id]['chidren'] = $category;
  9. }
  10. }
  11. return $categories;
  12. }
  13.  
  14. public static function make_index_id($source){
  15. $id_indexed_array = [];
  16. foreach ($source as $value) {
  17. $id_indexed_array[$value['id']] = $value;
  18. }
  19. return $id_indexed_array;
  20. }
  21.  
  22. // public static function makeBreadCump($source){
  23. // $nested = self::nestedCategories($source);
  24. // return $nested;
  25.  
  26. // $bread = [];
  27. // foreach ($source as $key => $category) {
  28. // if($key == 0){
  29. // if($category->parent_id){
  30.  
  31. // }
  32. // $bread[] = $category['name'];
  33. // }
  34. // }
  35. // dd($bread);
  36. // return $bread;
  37. // }
  38.  
  39. // public static function makeBreadCump($source){
  40. // $nested = self::nestedCategories($source);
  41. // dd($source);
  42. // $bread = [];
  43. // self::getBreadCumpArray($nested);
  44. // return self::$bread;
  45. // }
  46.  
  47. // public static function getBreadCumpArray($nested){
  48. // // dd($nested);
  49.  
  50. // foreach ($nested as $key => $nest) {
  51. // // dd($nest);
  52. // if($key == 0){
  53. // if(array_key_exists('children', $nest)){
  54. // // dd($nest['children']);
  55. // // self::$bread[] = 'hd';
  56. // self::$bread[] = $nest['name'];
  57. // self::getBreadCumpArray($nest['children']);
  58. // // self::$bread[] = $nest['children'][0]['name'];
  59. // } else {
  60. // // dd($nest['name']);
  61. // self::$bread[] = $nest['name'];
  62. // }
  63. // }
  64.  
  65. // }
  66. // }
  67.  
  68. public static function getNestedCategories(){
  69. $source = \App\BlogCategory::all();
  70. return self::nestedCategories($source);
  71. }
  72.  
  73. public static function nestedCategories($source) {
  74. $nested = [];
  75. $source = $source->toArray();
  76. // return $source;
  77. $indexed_id_array = self::make_index_id($source);
  78.  
  79. foreach ( $indexed_id_array as &$s ) {
  80. if ( is_null($s['parent_id']) ) {
  81. // no parent_id so we put it in the root of the array
  82. $nested[] = &$s;
  83. }
  84. else {
  85. $pid = $s['parent_id'];
  86. if ( isset($indexed_id_array[$pid]) ) {
  87. // dd($pid);
  88. // If the parent ID exists in the indexed_id_array array
  89. // we add it to the 'children' array of the parent after initializing it.
  90.  
  91. if ( !isset($indexed_id_array[$pid]['children']) ) {
  92. $indexed_id_array[$pid]['children'] = array();
  93. }
  94.  
  95. $indexed_id_array[$pid]['children'][] = &$s;
  96. }
  97. }
  98. }
  99. return $nested;
  100. }
  101.  
  102.  
  103. static function fetchNestedCategories($arr, $field_name='categories', $active_list=[], $indent='') {
  104. // dd($active_list);
  105. if ($arr) {
  106. foreach ($arr as $key => $value) {
  107. if (is_array($value)) {
  108. //
  109. self::fetchNestedCategories($value, $field_name, $active_list, $indent . '  ');
  110. } else {
  111. if($key == 'id'){
  112. if(in_array($arr[$key], $active_list)){
  113. $checked = 'checked';
  114. } else{
  115. $checked = '';
  116. }
  117.  
  118.  
  119. echo '<li>' . $indent . '<label><input value="'.$arr[$key].'" type="checkbox" name="'.$field_name.'[]" '. $checked.'>'.$arr['name'].'</label></li>';
  120. // echo "$indent $value <br />";
  121. }
  122. }
  123. }
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement