laravel 5 where() not working

i have two tables, categories and subcategories, and each item in subcategories belongs to categories by foreign key “category_id”.

i have two models one for categories and one for subcategories, and one controller.

my category model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    //
    public function subcategories()
    {
        return $this->hasMany('\App\Subcategory');
    }

}

subcategory model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Subcategory extends Model
{
    protected $fillable = [

        'sub_category_name',
        'category_id'
    ];
}

and CategoriesController

<?php

namespace App\Http\Controllers;

use App\Category;
use App\Subcategory;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CategoriesController extends Controller
{
    //return array of categories
    public function categories ()
    {

        $categories =  Category::where('category_name', '=', 'Construction Machines')->subcategories;
        return $categories;
    }
}

question is when i do Category::find()->subcategories i get the expected results. but when i use where(), it gives me Undefined property: Illuminate\Database\Eloquent\Builder::$subcategories error

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s