Laravel-belongsToMany relasi

17:16 Unknown 0 Comments

ketika saya punya model Role dan punya fungsi relasi belongsToMany r_permission().


 public function r_permission()
  {
return $this->belongsToMany(Permission::class,'permission_role','role_id','permission_id');
    }

penjelasan:

parameter1:nama class relasi tujuan

parameter2:tabel pivot

parameter3:lokal key

parameter4:foreign key

menggunakan nya:


jika insert ke tabel pivot


$permission = Permission::where('namaPermission', $permission)->first();

$role = App\Role::where('namaRole', 'hrd')->first();

$role->r_permission()->attach($permission);

0 comments: