简体   繁体   中英

How to get a value of multidimensional associative array from Laravel collection

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

The problem is in the $categories[0] => to get the first element you can use first() like this :

$sub_categories = $categories->first()->subCategories

Then you can loop over the sub categories :

foreach ($sub_categories  as $sub_category ) {
    // sub_category->name;
}

You can add a test if sub categories exists or not like this

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;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM