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

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -