What is the difference between BelongsTo And HasOne in Laravel

后端 未结 5 456
被撕碎了的回忆
被撕碎了的回忆 2020-12-23 16:19

Can any body tell me what is the main difference between
the BelongsTo and HasOne relationship in eloquent.

5条回答
  •  醉话见心
    2020-12-23 16:59

    If you want to make One TO one relationship between two table then first you have to make "hasOne" Relation and If you want to make inversely table relationship then you make " "Belongs to"... IT is a simple difference between HasOne and Belongs to the relationship if you want to know about this One To Many (Inverse)
    Now that we can access all of a post's comments, let's define a relationship to allow a comment to access its parent post. To define the inverse of a hasMany relationship, define a relationship function on the child model which calls the belongsTo method:

    belongsTo('App\Post');
        }
    }
    

提交回复
热议问题