jeudi 29 octobre 2015

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



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1Ml4ZQC
via IFTTT

Aucun commentaire:

Enregistrer un commentaire