https://laravelacademy.org/post/9584.html
一对一:
class User extends Model { public function profile() { return $this->hasOne(Profile::class); } } class Profile extends Model { public function user() { return $this->belongsTo(User::class); } }
一对多:
class Post extends Model {
public function comments() {
return $this->hasMany(Comment::class);
} }
class Comment extends Model {
public function post() {
return $this->belongsTo(Post::class);
} }
多对多:
class User extends Model { public function roles() { return $this->belongsToMany(Role::class); } } class Role extends Model { public function users() { return $this->belongsToMany(User::class); } }
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/112809.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...