我从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
数组。我知道在刀刃上我可以用点符号。我的控制器是这样的:
<?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
最佳答案
问题是在$categories[0]
=>中获取第一个元素,您可以使用如下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;
}
}
}