Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static function getCategories(){
- $get_categories = [];
- $categories = \App\BlogCategory::all();
- foreach ($categories as $category) {
- if($category->parent_id == null){// parent category
- $get_categories[$category->id] = $category;
- } else {
- $get_categories[$category->parent_id]['chidren'] = $category;
- }
- }
- return $categories;
- }
- public static function make_index_id($source){
- $id_indexed_array = [];
- foreach ($source as $value) {
- $id_indexed_array[$value['id']] = $value;
- }
- return $id_indexed_array;
- }
- // public static function makeBreadCump($source){
- // $nested = self::nestedCategories($source);
- // return $nested;
- // $bread = [];
- // foreach ($source as $key => $category) {
- // if($key == 0){
- // if($category->parent_id){
- // }
- // $bread[] = $category['name'];
- // }
- // }
- // dd($bread);
- // return $bread;
- // }
- // public static function makeBreadCump($source){
- // $nested = self::nestedCategories($source);
- // dd($source);
- // $bread = [];
- // self::getBreadCumpArray($nested);
- // return self::$bread;
- // }
- // public static function getBreadCumpArray($nested){
- // // dd($nested);
- // foreach ($nested as $key => $nest) {
- // // dd($nest);
- // if($key == 0){
- // if(array_key_exists('children', $nest)){
- // // dd($nest['children']);
- // // self::$bread[] = 'hd';
- // self::$bread[] = $nest['name'];
- // self::getBreadCumpArray($nest['children']);
- // // self::$bread[] = $nest['children'][0]['name'];
- // } else {
- // // dd($nest['name']);
- // self::$bread[] = $nest['name'];
- // }
- // }
- // }
- // }
- public static function getNestedCategories(){
- $source = \App\BlogCategory::all();
- return self::nestedCategories($source);
- }
- public static function nestedCategories($source) {
- $nested = [];
- $source = $source->toArray();
- // return $source;
- $indexed_id_array = self::make_index_id($source);
- foreach ( $indexed_id_array as &$s ) {
- if ( is_null($s['parent_id']) ) {
- // no parent_id so we put it in the root of the array
- $nested[] = &$s;
- }
- else {
- $pid = $s['parent_id'];
- if ( isset($indexed_id_array[$pid]) ) {
- // dd($pid);
- // If the parent ID exists in the indexed_id_array array
- // we add it to the 'children' array of the parent after initializing it.
- if ( !isset($indexed_id_array[$pid]['children']) ) {
- $indexed_id_array[$pid]['children'] = array();
- }
- $indexed_id_array[$pid]['children'][] = &$s;
- }
- }
- }
- return $nested;
- }
- static function fetchNestedCategories($arr, $field_name='categories', $active_list=[], $indent='') {
- // dd($active_list);
- if ($arr) {
- foreach ($arr as $key => $value) {
- if (is_array($value)) {
- //
- self::fetchNestedCategories($value, $field_name, $active_list, $indent . ' ');
- } else {
- if($key == 'id'){
- if(in_array($arr[$key], $active_list)){
- $checked = 'checked';
- } else{
- $checked = '';
- }
- echo '<li>' . $indent . '<label><input value="'.$arr[$key].'" type="checkbox" name="'.$field_name.'[]" '. $checked.'>'.$arr['name'].'</label></li>';
- // echo "$indent $value <br />";
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement