I have generate the following database table and need to create model form them.
Creating a model seems to be an easy task, but I am confused how should i define the relationship between two tables. In users
table RoleId
is foreign key. My question is where and how should I define the relationship (in User model or Role model). Should I use hasOne
, hasMany
, or belongsTo
In your User model
public function roles()
{
return $this->hasOne('Role', 'id', 'RoleId');
}
In your Role model
public function users()
{
return $this->belongsTo('User', 'RoleId', 'id);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.