Skip to content

Instantly share code, notes, and snippets.

@wichaksono
Created March 15, 2018 00:55
Show Gist options
  • Save wichaksono/afa84b461285bf735ecaa207f4358463 to your computer and use it in GitHub Desktop.
Save wichaksono/afa84b461285bf735ecaa207f4358463 to your computer and use it in GitHub Desktop.
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/*
* Method untuk yang mendefinisikan relasi antara model user dan model Role
*/
public function roles()
{
return $this->belongsToMany(Role::class);
}
/*
* Method untuk menambahkan role (hak akses) baru pada user
*/
public function putRole($role)
{
if (is_string($role))
{
$role = Role::whereRoleName($role)->first();
}
return $this->roles()->attach($role);
}
/*
* Method untuk menghapus role (hak akses) pada user
*/
public function forgetRole($role)
{
if (is_string($role))
{
$role = Role::whereRoleName($role)->first();
}
return $this->roles()->detach($role);
}
/*
* Method untuk mengecek apakah user yang sedang login punya hak akses untuk mengakses page sesuai rolenya
*/
public function hasRole($roleName)
{
foreach ($this->roles as $role)
{
if ($role->role_name === $roleName) return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment