[英]How to get child under sub-category and sub-category user category in JSON Laravel
This very common question but I am not able to make it in JSON Format for API.这是一个非常常见的问题,但我无法将其设为 API 的 JSON 格式。 I a Table Category where I stored three layers of Category.我是一个表类别,我在其中存储了三层类别。 Category->subcategory->child.类别->子类别->子类别。 I want to get this in API in Format.我想以格式在 API 中获取它。
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我得到了这个结果不是我不知道如何获得孩子的价值
database look like数据库看起来像
Here is how you can do it.这是您如何做到的。
$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;
//children of subcat
$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;
}
}
}
// set subcategory array
$allData[$key]['subcategory'] = $subCat;
}
return $allData;
please try this way.请尝试这种方式。 convert your json into array and use this to get child values将您的 json 转换为数组并使用它来获取子值
$ar = json_decode($data,true);
foreach($ar as $value){
echo $value['parent'].'<br>';
foreach($value['child'] as $child){
echo ' '.$child.'<br>';
}
}
This will output this这将输出这个
test
A
B
C
D
E
test1
G
H
I
J
K
Finally, I solved this最后,我解决了这个问题
$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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.