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
wheretblsubcategory
.category__model_id
= 1 andtblsubcategory
.category__model_id
is not null)
belongsTo
method defined – Gino Panesubcategory
to itscategory
? – Tim LewishasMany
andbelongsTo
always works for me, that's why I'm suggesting to try it. – Gino Panecategory_model_id
, but more likelycategory_id
– Tim Lewis