Relation trong laravel: belongsTo, hasOne, belongsToMany, hasMany, morphTo, morphOne, morphMany, morphedByMany, pivot, associate, attach và sync

04/01/2022   Laravel
Relation trong laravel: belongsTo, hasOne, belongsToMany, hasMany, morphTo, morphOne, morphMany, morphedByMany, pivot, associate, attach và sync
1. FK trong table chính (phones)
- Users: id
- Phones: id, user_id
=> có các loại quan hệ
* hasOne: user hasOne phone
=> $user->phone: phone() { return $this->hasOne(Phone::class, 'foreign_key', 'local_key'); }
* local_key: local tức là đang strong model User => local_key là cột trong bảng user, trường hợp này là id của bảng user (nếu ko muốn dùng id, có thể dùng cột khác)
* foreign_key: là FK của bảng có FK, ở đây là Phone::class, muốn sử dụng để so sánh với local_key, để lấy row trong bảng phones.
=> $user->phone: phone() { return $this->hasOne(Phone::class, 'user_id', 'id'); } tức là: tìm row trong table phones mà gía trị cột phones.user_id == users.id
* belongsTo: phone belongsTo user
=> $phone->user: user() { return $this->belongsTo(User:class, 'foreign_key', 'other_key'); }
=> $phone->user: user() { return $this->belongsTo(User:class, 'user_id', 'id'); }
=> associate()
- $user = User::find(1); $phone->user()->associate($user); $phone->save(); => add FK for table phones
- $phone->user()->dissociate(); $phone->save(); => FK is Null
* hasMany: user hasMany phone
=> $user->phones: phones() { return $this->hasMany(Phone::class, 'foreign_key', 'local_key'); }
=> $user->phones: phones() { return $this->hasMany(Phone::class, 'user_id', 'id'); }
2. FK trong table trung gian (intermediate table pivot: role_user)
- users: id
- roles: id
- role_user: id, user_id, role_id, active, created_by
* Pivot
- Pivot là table chung của 2 table trong quan hện N-N
=> pivot: role_user
* belongsToMany: user belongsToMany roles
=> $user->roles(): roles() { return $this->belongsToMany(Role::class, 'role_user', 'user_id', 'role_id'); }
=> $role->users(): users() { return $this->belongsToMany(User::class, 'role_user', 'role_id', 'user_id'); }
=> $user->roles(): roles() { return $this->belongsToMany(Role::class, 'role_user', 'user_id', 'role_id')->withPivot('active', 'created_by'); }
=> attach()
- $user->roles()->attach($roleId, ['active' => $active]);
- $user->roles()->attach([ 1 => ['active' => $active1], 2 => ['active' => $active2] ]);
=> sync()
- $user->roles()->sync([1, 2, 3]);
- $user->roles()->sync([1 => ['expires' => true], 2, 3]);
- $user->roles()->syncWithPivotValues([1, 2, 3], ['active' => true]);
- $user->roles()->syncWithoutDetaching([1, 2, 3]);
3. Quan hệ đa hình Polymorphic
* FK trong table chính (Phones)
- Users: id
- Admins: id
- Phones: id, phoneable_id, phoneable_type
+ phoneable_id: Id của User hoặc Admin
+ phoneable_type: tên Class của User hoặc Admin
* morphOne
=> $user->phone(): phone() { return $this->morphOne(Phone::class, 'phoneable'); }
* morphMany
=> $user->phone(): phones() { return $this->morphMany(Phone::class, 'phoneable'); }
* FK trong table trung gian (intermediate table pivot: roleables)
- users: id
- admins: id
- roles: id
- roleables: role_id, roleable_id, roleable_type
* morphToMany
=> $user->roles(): roles() { return $this->morphToMany(Role::class, 'roleable'); }
* morphedByMany
=> $role->users(): users(){ return $this->morphedByMany(User::class, 'roleable');}
=> $role->admins(): admins(){ return $this->morphedByMany(Admin::class, 'roleable');}
Cám ơn
Tham khảo: https://laravel.com/docs/9.x/eloquent-relationships

