Role.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: sdf_sky
  5. * Date: 16/6/20
  6. * Time: 下午6:14
  7. */
  8. namespace App\Models;
  9. use Illuminate\Database\Eloquent\Model;
  10. class Role extends Model
  11. {
  12. protected $table = 'roles';
  13. protected $fillable = ['id', 'name','description','sort','slug'];
  14. public function permissions()
  15. {
  16. return $this->belongsToMany('App\Models\Permission');
  17. }
  18. /**
  19. * Determine if the role has the given permission.
  20. *
  21. * @param mixed $permission
  22. * @return boolean
  23. */
  24. public function inRole($permission)
  25. {
  26. if (is_string($permission)) {
  27. return $this->permissions->contains('name', $permission);
  28. }
  29. return !! $permission->intersect($this->permissions)->count();
  30. }
  31. /**
  32. * @param $permission
  33. * @return bool
  34. */
  35. public function attachPermission($permission)
  36. {
  37. return (!$this->permissions()->get()->contains($permission)) ? $this->permissions()->attach($permission) : true;
  38. }
  39. /**
  40. * @param $permission
  41. * @return mixed
  42. */
  43. public function detachPermission($permission)
  44. {
  45. return $this->permissions()->detach($permission);
  46. }
  47. /**
  48. * Detach all permissions.
  49. *
  50. * @return int
  51. */
  52. public function detachAllPermissions()
  53. {
  54. return $this->permissions()->detach();
  55. }
  56. }