如何在JSON Laravel中获取子类别和子类别用户类别下的子类别?

huangapple go评论84阅读模式
英文:

How to get child under sub-category and sub-category user category in JSON Laravel

问题

这是一个非常常见的问题,但我无法将其以JSON格式制作成API。我有一个名为Category的表,其中存储了三层类别。类别->子类别->子类。
我希望在API中以以下格式获取这些数据。

类别

子类别1
子类别2
子类别2-1
子类别2-2
子类别3

我尝试了以下代码:

$allData = array();
// 获取所有父类别
$categories = Category::where(['status' => 1, 'parent_id' => 0])->get();
foreach ($categories as $key => $sub) {
    // 逐个获取子类别
    $allData[$key]['parent'] = $sub->name;
    $subCategory = Category::where('status', 1)->where('parent_id', '=', $sub->id)->get();
    $subCat = array();
    // 设置父类别标题
    foreach ($subCategory as $k => $subcat) {
        $subCat[$subcat->id] = $subcat->name;
    }
    // 设置子类别数组
    $allData[$key]['subcategory'] = $subCat;
}
return $allData;

我得到了这个结果,但我不知道如何获取子类别的值

英文:

This very common question but I am not able to make it in JSON Format for API. I a Table Category where I stored three layers of Category. Category->subcategory->child.
I want to get this in API in Format.

Something like.

Category

  sub-category-1
  sub-category-2 
      child-2-1
      child-2-2
  sub-category-3

I try with code

  $allData = array();
            // get all parent category 
            $categories = Category::where(['status' => 1, 'parent_id' => 0])->get();        
            foreach ($categories as $key=>$sub) {
                    // now take one by one it's child category 
                    $allData[$key]['parent'] = $sub->name;
                    $subCategory = Category::where('status', 1)->where('parent_id', '=', $sub->id)->get();
                    $subCat = array();
                    // set parent category title
                    foreach ($subCategory as $k=>$subcat) {
                        $subCat[$subcat->id] = $subcat->name;

                    }
                    // set subcategory array
                    $allData[$key]['subcategory'] = $subCat;
            }
           return $allData;

I got this result not I don't know how to get child value

如何在JSON Laravel中获取子类别和子类别用户类别下的子类别?

database look like

如何在JSON Laravel中获取子类别和子类别用户类别下的子类别?

答案1

得分: 1

这是如何完成的示例。

$allData = array();
// 获取所有父类别
$categories = Category::where(['status' => 1, 'parent_id' => 0])->get();        
foreach ($categories as $key => $sub) {
    // 逐个获取其子类别
    $allData[$key]['parent'] = $sub->name;
    $subCategory = Category::where('status', 1)->where('parent_id', '=', $sub->id)->get();
    $subCat = array();
    // 设置父类别标题
    foreach ($subCategory as $k => $subcat) {
        $subCat[$subcat->id] = $subcat->name;
        // 子类别的子类别
        $children = Category::where('status', 1)->where('parent_id', '=', $subcat->id)->get();	
        $child = array();
        if ($children) {
            foreach ($children as $k1 => $val) {
                $child[$val->id] = $val->name;
                $allData[$key]['subcategory'][$subcat->id]['child'] = $child;
            }
        }
    }
    // 设置子类别数组
    $allData[$key]['subcategory'] = $subCat;
}
return $allData;

请尝试这种方式。将您的 JSON 转换为数组,并使用以下方式获取子值。

$ar = json_decode($data, true);
foreach ($ar as $value) {
    echo $value['parent'].'<br>';
    foreach ($value['child'] as $child) {
        echo '&nbsp;'.$child.'<br>';
    }
}

这将输出如下结果:

test
 A
 B
 C
 D
 E
test1
 G
 H
 I
 J
 K
英文:

Here is how you can do it.

 $allData = array();
        // get all parent category 
        $categories = Category::where([&#39;status&#39; =&gt; 1, &#39;parent_id&#39; =&gt; 0])-&gt;get();        
        foreach ($categories as $key=&gt;$sub) {
                // now take one by one it&#39;s child category 
                $allData[$key][&#39;parent&#39;] = $sub-&gt;name;
                $subCategory = Category::where(&#39;status&#39;, 1)-&gt;where(&#39;parent_id&#39;, &#39;=&#39;, $sub-&gt;id)-&gt;get();
                $subCat = array();
                // set parent category title
                foreach ($subCategory as $k=&gt;$subcat) {
                    $subCat[$subcat-&gt;id] = $subcat-&gt;name;
					//children of subcat 
					$children = Category::where(&#39;status&#39;, 1)-&gt;where(&#39;parent_id&#39;, &#39;=&#39;, $subcat-&gt;id)-&gt;get();	
					$child = array();
					if($children){
						foreach($children as $k1=&gt;$val){
							 $child[$val-&gt;id] = $val-&gt;name;
                             $allData[$key][&#39;subcategory&#39;][$subcat-&gt;id][&#39;child&#39;] = $child;
						}
					}
					
                }
                // set subcategory array
                $allData[$key][&#39;subcategory&#39;] = $subCat;
        }
       return $allData;

please try this way. convert your json into array and use this to get child values

$ar = json_decode($data,true);
foreach($ar as $value){
	echo $value[&#39;parent&#39;].&#39;&lt;br&gt;&#39;;
    foreach($value[&#39;child&#39;] as $child){
	
	   echo &#39;&amp;nbsp;&#39;.$child.&#39;&lt;br&gt;&#39;;
   }
 }

This will output this

test
 A
 B
 C
 D
 E
test1
 G
 H
 I
 J
 K

答案2

得分: 1

最终,我解决了这个问题:

$parents = Category::where('parent_id', 0)->where('status', 1)->orderBy('sort_order', 'asc')->get();
foreach ($parents as $parent) {
    $childs = Category::where('parent_id', $parent->id)->where('status', 1)->orderBy('sort_order', 'asc')->get();
    if (count($childs) > 0) {
        $subCat = array();
        $players = array();
        $roster[$parent->name] = $players;
        foreach ($childs as $i => $child) {
            $subchilds = Category::where('parent_id', $child->id)->where('status', 1)->orderBy('sort_order', 'asc')->get();
            if (count($subchilds) > 0) {
                $roster[$parent->name][$child->name] = $subCat;
                foreach ($subchilds as $subchild) {
                    $roster[$parent->name][$child->name][$subchild->id] = $subchild->name;
                }
            } else {
                $roster[$parent->name][$child->name] = $players;
            }
        }
    }
}
return $roster;

如何在JSON Laravel中获取子类别和子类别用户类别下的子类别?

英文:

Finally, I solved this

       $parents = Category::where(&#39;parent_id&#39;, 0)-&gt;where(&#39;status&#39;, 1)-&gt;orderBy(&#39;sort_order&#39;, &#39;asc&#39;)-&gt;get();
        foreach ($parents as $parent) {
            $childs = Category::where(&#39;parent_id&#39;, $parent-&gt;id)-&gt;where(&#39;status&#39;, 1)-&gt;orderBy(&#39;sort_order&#39;, &#39;asc&#39;)-&gt;get();
            if (count($childs) &gt; 0) {
                $subCat = array();
                $players = array();
                $roster[$parent-&gt;name] = $players;
                        foreach ($childs as $i =&gt; $child) {
                            $subchilds = Category::where(&#39;parent_id&#39;, $child-&gt;id)-&gt;where(&#39;status&#39;, 1)-&gt;orderBy(&#39;sort_order&#39;, &#39;asc&#39;)-&gt;get();
                            if (count($subchilds) &gt; 0) {
                              
                                $roster[$parent-&gt;name][$child-&gt;name] = $subCat;
                                foreach ($subchilds as $subchild) {
                                   
                                    $roster[$parent-&gt;name][$child-&gt;name][$subchild-&gt;id] = $subchild-&gt;name;
                                }
                               
                            }else{
                                $roster[$parent-&gt;name][$child-&gt;name] = $players;
                            }
                        }
               
            }
        }
        return $roster;

如何在JSON Laravel中获取子类别和子类别用户类别下的子类别?

huangapple
  • 本文由 发表于 2020年1月3日 19:34:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577909.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定