I'm returning a collection from Laravel application which then looks like this.
[
{
"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": []
}
]
Its basically a dynamic menu with primary menu links as well as sub links. Now the issue is that I want to access the array of sub_categories from my controller not my blade file. I know in blade i could use the dot "." notation.
This is how my controller looks like:
<?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;
}
}
So far i tried this is what i tried
1) $categories[0]["SubCategories"]
2) $categories[0]->SubCategories
.operator$sub_categories = $categories[0]["sub_categories"]then i tried using'->'like this$sub_categories = $categories[0]->sub_categories.