我从Laravel应用程序返回一个集合,然后看起来像这样。
[
{
"id": 1,
"name": "Business",
"created_at": "2017-10-16 15:11:47",
"updated_at": "2017-10-16 15:11:47",
"sub_categories": [
{
"id": 1,
"name": "Accounting",
"category_id": 1,
"created_at": "2017-10-16 15:12:41",
"updated_at": "2017-10-16 15:12:41"
},
{
"id": 2,
"name": "Business Information Systems",
"category_id": 1,
"created_at": "2017-10-16 15:12:41",
"updated_at": "2017-10-16 15:12:41"
},
{
"id": 5,
"name": "Business Law",
"category_id": 1,
"created_at": "2017-10-22 15:48:31",
"updated_at": "2017-10-22 15:48:31"
},
{
"id": 9,
"name": "Business Policy and Strategy",
"category_id": 1,
"created_at": "2017-10-22 15:49:20",
"updated_at": "2017-10-22 15:49:20"
},
{
"id": 11,
"name": "Business Research",
"category_id": 1,
"created_at": "2017-10-22 15:49:33",
"updated_at": "2017-10-22 15:49:33"
},
{
"id": 4,
"name": "Finance",
"category_id": 1,
"created_at": "2017-10-16 15:13:04",
"updated_at": "2017-10-16 15:13:04"
},
{
"id": 6,
"name": "Management",
"category_id": 1,
"created_at": "2017-10-22 15:48:31",
"updated_at": "2017-10-22 15:48:31"
},
{
"id": 8,
"name": "Managerial Skills & Communication",
"category_id": 1,
"created_at": "2017-10-22 15:48:58",
"updated_at": "2017-10-22 15:48:58"
},
{
"id": 10,
"name": "Marketing",
"category_id": 1,
"created_at": "2017-10-22 15:49:20",
"updated_at": "2017-10-22 15:49:20"
},
{
"id": 3,
"name": "Microeconomics",
"category_id": 1,
"created_at": "2017-10-16 15:13:04",
"updated_at": "2017-10-16 15:13:04"
},
{
"id": 7,
"name": "Organisational Behaviour",
"category_id": 1,
"created_at": "2017-10-22 15:48:58",
"updated_at": "2017-10-22 15:48:58"
}
]
},
{
"id": 2,
"name": "Engineering",
"created_at": "2017-10-16 15:11:47",
"updated_at": "2017-10-16 15:11:47",
"sub_categories": [
{
"id": 12,
"name": "Aerospace Engineering",
"category_id": 2,
"created_at": "2017-10-22 16:46:57",
"updated_at": "2017-10-22 16:46:57"
},
{
"id": 13,
"name": "Automotive Engineering",
"category_id": 2,
"created_at": "2017-10-22 16:46:57",
"updated_at": "2017-10-22 16:46:57"
},
{
"id": 14,
"name": "Civil Engineering",
"category_id": 2,
"created_at": "2017-10-22 16:47:38",
"updated_at": "2017-10-22 16:47:38"
},
{
"id": 15,
"name": "Communication Systems",
"category_id": 2,
"created_at": "2017-10-22 16:47:38",
"updated_at": "2017-10-22 16:47:38"
},
{
"id": 16,
"name": "Computer and Internet Engineering",
"category_id": 2,
"created_at": "2017-10-22 16:48:38",
"updated_at": "2017-10-22 16:48:38"
},
{
"id": 17,
"name": "Electrical and Electronic Engineering",
"category_id": 2,
"created_at": "2017-10-22 16:48:38",
"updated_at": "2017-10-22 16:48:38"
},
{
"id": 18,
"name": "Mechanical Engineering",
"category_id": 2,
"created_at": "2017-10-22 16:48:50",
"updated_at": "2017-10-22 16:48:50"
}
]
},
{
"id": 3,
"name": "Education",
"created_at": "2017-10-22 15:05:51",
"updated_at": "2017-10-22 15:05:51",
"sub_categories": []
},
{
"id": 4,
"name": "Health Sciences",
"created_at": "2017-10-22 15:05:51",
"updated_at": "2017-10-22 15:05:51",
"sub_categories": []
},
{
"id": 5,
"name": "Science",
"created_at": "2017-10-22 15:06:26",
"updated_at": "2017-10-22 15:06:26",
"sub_categories": []
},
{
"id": 6,
"name": "Humanities",
"created_at": "2017-10-22 15:06:26",
"updated_at": "2017-10-22 15:06:26",
"sub_categories": []
},
{
"id": 7,
"name": "Social Sciences",
"created_at": "2017-10-22 15:06:43",
"updated_at": "2017-10-22 15:06:43",
"sub_categories": []
},
{
"id": 8,
"name": "Medicine",
"created_at": "2017-10-22 15:06:43",
"updated_at": "2017-10-22 15:06:43",
"sub_categories": []
}
]
它基本上是一个带有主菜单链接和子链接的动态菜单。现在问题是我想从我的控制器而不是我的sub_categories
访问blade file
数组。我知道在刀片上我可以使用点"。"符号
这就是我的控制器的样子:
<?php
namespace App\Http\Controllers;
use App;
use App\Category;
use App\SubCategory;
use Illuminate\Http\Request;
class SeriesController extends Controller
{
public function getSeries()
{
$categories = Category::with(['SubCategories'])->get();
return $categories;
}
}
到目前为止,我试过这是我试过的
1) $categories[0]["SubCategories"]
2) $categories[0]->SubCategories
答案 0 :(得分:2)
问题出在$categories[0]
=&gt;获取第一个元素,你可以使用first()
这样:
$sub_categories = $categories->first()->subCategories
然后你可以遍历子类别:
foreach ($sub_categories as $sub_category ) {
// sub_category->name;
}
如果子类别存在与否,您可以添加测试
foreach ($categories as $category ) {
if(isset($category->subCategories) && $category->subCategories->count()>0) {
foreach ($category->subCategories as $sub_category ) {
echo 'category name : '. $category->name .' has sub cat name : '. $sub_category->name;
}
}
}