Laravel Eloquent Self Join Parent Child -
laravel eloquent self join parent child relationship
class product_category extends model { // protected $table='product_categories'; public $timestamps = false; public function getparentcategory() { return $this->hasone(self::class, 'id', 'parent_id'); }. public function getchildcategories(){ return $this->hasmany(self::class, 'parent_id','id'); }
i able retrieve child records of table making use of getchildecategories not able retrieve parent of particular category. gives me null.
it's called one-to-many relationship, inverse of hasmany
relationship belongsto
method, getparentcategory()
should be:
public function getparentcategory() { return $this->belongsto(self::class, 'parent_id'); } public function getchildcategories(){ return $this->hasmany(self::class, 'parent_id'); }
Comments
Post a Comment