3

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
7
  • Use dot . operator Commented Oct 23, 2017 at 9:27
  • Show us your code !! Commented Oct 23, 2017 at 9:30
  • I stored the collection inside a variable called "$categories", then i tried to access the "sub_categories" value by doing $sub_categories = $categories[0]["sub_categories"] then i tried using '->' like this $sub_categories = $categories[0]->sub_categories. Commented Oct 23, 2017 at 9:31
  • 1
    @JulianTabona put it in the question. Commented Oct 23, 2017 at 9:32
  • @mega6382 noted, let me do that Commented Oct 23, 2017 at 9:33

1 Answer 1

2

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;
        }
    }
}
Sign up to request clarification or add additional context in comments.

12 Comments

I was hoping that would work. When i return $categories->first() i definetly get the first element, however when i do $categories->first()->sub_categories i get a blank screen.
Try it with $categories->first()["sub_categories"] and is the first element have sub categories ?
This is weird, i tried $categories->first()->name and it works, i tried $categories->first()->created_at and $categories->first()->updated_at and they both worked but the $categories->first()->sub_categories returns nothing.... is there a special rule we are missing?
I think because the first category don't have sub categories !! try dd($categories->first()); to check it !!
I'm trying to paste the $categories result, but the comment editor has char limits, how can i send to you? Sorry still new with using stackoverflow
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.