hasMany vs belongsToMany in laravel 5.x

后端 未结 2 1157
情歌与酒
情歌与酒 2020-12-13 00:07

I\'m curious why the Eloquent relationship for hasMany has a different signature than for belongsToMany. Specifically the custom join table name--

2条回答
  •  萌比男神i
    2020-12-13 00:28

    Try to understand with text and a figure.

    1. One to One(hasOne) relationship:

      • A user has(can have) one profile. So, a profile belongs to one user.
    2. One to many(hasMany):

      • A user has many(can have many) articles. So, many articles belong to one user.
    3. Many to many(BelongsToMany):

      • A User can belong to many forums. So, a forum belongs to many users.

提交回复
热议问题