laravel eloquent orm
[one to one]
Table: User, Phone
Model: User
Relation: return $this->hasOne(‘App\Phone’);
Talbe Phone need have user_id. Auto use User id -> Phone user_id
Table: User, Phone
Model: Phone
Relaion: return $this->belongsTo(‘App\User’);
Table Phone need have user_id. Auto use User id -> User user_id
[one to many]
Table: Post, Comment
Model: Post
Relation: return $this->hasMany(‘App\Comment’);
Table Comment need have post_id. Auto use Post id -> Comment post_id
Table: Post, Comment
Model: Comment
Relaion: return $this->belongsTo(‘App\Post’);
Table Comment need have post_id. Auto Post id -> Comment post_id
[Many To Many]
Table: User, Role, Role_User
Table Role_User must have user_id, role_id
Model: User
way1:
Relaion: return $this->belongsToMany(‘App\Role’, ‘role_user’);
way2:
Relaion: return $this->belongsToMany(‘App\Role’);
Must database/migrations defined talbe class like “CreateRoleUserPivotTable”