Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 1

quan hệ 1-1 <?php <?

php
(hasOne ; class Phone extends Model
-belongTo) class User extends Model { {
Vd: một người public function phone() { public function user()
có một điện return $this->hasOne('App\Phone’); {
thoại } return $this->belongsTo('App\User');
}
}
Quan hệ 1- class Post extends Model class Comment extends Model
nhiều { {
(hasMany- public function comments() public function post()
belongTo) { {
vd: 1 blog postreturn $this->hasMany('App\Comment'); return $this->belongsTo('App\Post');
có nhiều } }
comment }
Quan hệ nhiều class User extends Model class Role extends Model
-nhiều { {
(belongtoMany public function roles() public function users()
- { {
belongtoMany) return $this->belongsToMany('App\ return $this->belongsToMany('App\
Role'); User');
} }
}
Has to thoungh class Country extends Model { public Countries: id – integer, name – string
(thông qua function posts() Users: id – integer, country_id –
trung gian để { integer, name – string
truy vấn) return $this->hasManyThrough('App\ Posts: id – integer, user_id -
Post', 'App\User'); } } integer ,title – string
Cần lấy các posts của một country
thông qua bảng trung gian users
Quan hệ nhiều- vd: quan hệ user va role
nhiều 1 user có nhiều roles
1 role cũng thuộc về nhiều user
cần phải có 3 bảng : user, role và
user_role
bảng user_role_ chứa 2 cột user_id và
role_id

You might also like