0
votes

What am I trying?

Get a particular category and it's associated Sub Categories


Category Model

class Category_Model extends Model
{
    protected $table = "tblcategory";
    protected $primaryKey = "CategoryID";
    public $timestamps = false;

    public function Subcategories()
    {
        return $this->hasMany('\App\Models\Skill\SubCategory_Model');
    }
}

Sub Category Model

class SubCategory_Model extends Model
{
    protected $table = "tblsubcategory";
    protected $primaryKey = "SubCategoryID";
    public $timestamps = false;
}

Action Method

public function SubCategories($category)
{
    $Categories = \App\Models\Skill\Category_Model
                  ::where("Category", "=", $category)
                  ->Subcategories;
    dd($Categories);
}

When I run the code. I get below error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'tblsubcategory.category__model_id' in 'where clause' (SQL: select * from tblsubcategory where tblsubcategory.category__model_id = 1 and tblsubcategory.category__model_id is not null)

1
It seems that SubCategories must have belongsTo method definedGino Pane
@GinoPane No, it doesn't have to, but it is good practice. Helper, what is the column that links a subcategory to its category?Tim Lewis
@TimLewis I believe, that second parameter must be defined if foreign keys have non-conventional names or sth like that. Actually combination of hasMany and belongsTo always works for me, that's why I'm suggesting to try it.Gino Pane
@GinoPane Yes, you are correct about the naming. I was merely stating that you don't need to define the inverse. I suspect the column name isn't category_model_id, but more likely category_idTim Lewis
I am trying category record and also it's sub categories. Am I missing something ?Pankaj

1 Answers

3
votes

From the comments, it is most likely that your subcategory table doesn't have a category_model_id but likely a category_id. By default, Laravel tries to extrapolate the name of the foreign column from the model name (in this case Category_Model, which explains the category_model_id. Changing the class to:

class Category_Model extends Model
{
    protected $table = "tblcategory";
    protected $primaryKey = "CategoryID";
    public $timestamps = false;

    public function Subcategories()
    {
        return $this->hasMany('\App\Models\Skill\SubCategory_Model', 'category_id')->get(); // Or whatever the column is actually called
    }
}

should solve the issue.

To return both the category object and its subcategories, change the action code to:

public function SubCategories($category)
{
    $Categories = \App\Models\Skill\Category_Model
                  ::where("Category", "=", $category)
                  ->with("Subcategories")
                  ->first();
    dd($Categories);
} 

The $Categories should also now contain a Subcategories object, accessed via $Categories->Subcategories, which should return a collection of Subcategory objects. If you wanted to see each one, you would loop through with a foreach

foreach($Categories->Subcategories AS $Subcategory){
  echo $Subcategory->name;
  // etc etc.
}