Laravel雄辩的自我加入父母子女

编程入门 行业动态 更新时间:2024-10-15 20:16:12
本文介绍了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雄辩的自我加入父母子女

本文发布于:2023-10-28 03:55:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535440.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:雄辩   子女   父母   自我   Laravel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!