本文介绍了Laravel雄辩的自我加入父母子女的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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');
}
我能够通过使用getChildeCategories来检索表的所有子记录,但无法检索特定类别的Parent. 它总是给我空值.
I am able to retrieve all child records of table by making use of getChildeCategories but not able to retrieve Parent of particular category. It always gives me null.
推荐答案这称为一对多关系,hasMany关系的逆是belongsTo方法,您的getParentCategory()应为:
It's called one-to-many relationship, the inverse of a hasMany relationship is belongsTo method, your getParentCategory() should be:
public function getParentCategory() { return $this->belongsTo(self::class, 'parent_id'); } public function getChildCategories(){ return $this->hasMany(self::class, 'parent_id'); }更多推荐
Laravel雄辩的自我加入父母子女
发布评论