S M Abrar Jahin
S M Abrar Jahin

Reputation: 14588

Showing Category And Subcategory In Laravel

I am using Laravel 5.2. I have 2 Eloquent Models like this-

Category.php-

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $table        =   'categories';           //Table Name
    public $timestamps      =   false;
    public $incrementing    =   false;                  //For Non integer Primary key
    protected $primaryKey   =   'name';

    protected $fillable     =   [
                                    'name'
                                ];

    public function SubCategory()
    {
        return $this->hasMany('App\SubCategory', 'category_id', 'id');
    }
}

And SubCategory.php-

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class SubCategory extends Model
{
    protected $table    =   'sub_categories';           //Table Name
    public $timestamps  =   false;

    protected $fillable     =   [
                                    'category_id',
                                    'name'
                                ];
}

So, now if I call this in the controller-

return Category::with('SubCategory')->get();

I am getting something like this-

[
  {
    "id": 3,
    "name": "Beahan-Mueller",
    "sub_category": [
      {
        "id": 27,
        "category_id": 3,
        "name": "Carroll Trail"
      },
      {
        "id": 3,
        "category_id": 3,
        "name": "Davis Lake"
      },
      {
        "id": 9,
        "category_id": 3,
        "name": "Lehner Ranch"
      }
    ]
  },
  {
    "id": 10,
    "name": "Beahan, Stark and McKenzi",
    "sub_category": [
      {
        "id": 1,
        "category_id": 10,
        "name": "Dibbert Summit"
      },
      {
        "id": 18,
        "category_id": 10,
        "name": "Kris Mount"
      }
    ]
  }
]

So, I can tell that sub-category link is working, right?

But my problem is if I want to use that with blade to show values like this-

Controller-

return view('public.listing.main', [
                                        'current_page'          =>  'Add Listing',
                                        'categories'            =>  Category::with('SubCategory')->get()
                                    ]);

View-

@foreach ($categories as $category)
    <li class="no-border">
        <label class="pull-left">
            <input type="checkbox" name="cat_{{ $category->id }}" checked>
            <strong> {{ $category->name }} (21)</strong>
        </label>
        <ul>
            @foreach($category->sub_category as $sub_cat)
                <li>
                    <label class="pull-left">
                        <input type="checkbox" checked value="{{ $sub_cat->id }}"> {{ $sub_cat->name }} (7)
                    </label>
                </li>
            @endforeach
        </ul>

    </li>
@endforeach

I am finding error like it-

Laravel Error

Can anyone please help, why I am finding this error?

Upvotes: 4

Views: 9175

Answers (1)

Davor Minchorov
Davor Minchorov

Reputation: 2076

Your subCategory relationship name is wrong in the 2nd foreach. It should be

@foreach($category->subCategory as $sub_cat)
    // code here
@endforeach

instead of sub_category.

Upvotes: 3

Related Questions