Roles.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. declare(strict_types=1);
  3. namespace Modules\Permissions\Models;
  4. use Catch\Base\CatchModel as Model;
  5. use Illuminate\Database\Eloquent\Collection;
  6. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  7. /**
  8. * @property $role_name
  9. * @property $identify
  10. * @property $parent_id
  11. * @property $description
  12. * @property $data_range
  13. * @property $creator_id
  14. * @property $created_at
  15. * @property $updated_at
  16. * @property $deleted_at
  17. */
  18. class Roles extends Model
  19. {
  20. protected $table = 'roles';
  21. protected $fillable = ['id', 'role_name', 'identify', 'parent_id', 'description', 'data_range', 'creator_id', 'created_at', 'updated_at', 'deleted_at'];
  22. /**
  23. * @var array
  24. */
  25. protected array $fields = ['id', 'role_name','identify','parent_id','description','data_range', 'created_at', 'updated_at'];
  26. /**
  27. * @var array
  28. */
  29. protected array $form = ['role_name','identify','parent_id','description','data_range'];
  30. protected array $formRelations = ['permissions', 'departments'];
  31. /**
  32. * @var bool
  33. */
  34. protected bool $isPaginate = false;
  35. /**
  36. * @var array
  37. */
  38. public array $searchable = [
  39. 'role_name' => 'like',
  40. 'id' => '<>'
  41. ];
  42. protected bool $asTree = true;
  43. /**
  44. *
  45. * @return BelongsToMany
  46. */
  47. public function permissions(): BelongsToMany
  48. {
  49. return $this->belongsToMany(Permissions::class, 'role_has_permissions', 'role_id', 'permission_id');
  50. }
  51. /**
  52. * departments
  53. *
  54. * @return BelongsToMany
  55. */
  56. public function departments(): BelongsToMany
  57. {
  58. return $this->belongsToMany(Departments::class, 'role_has_departments', 'role_id', 'department_id');
  59. }
  60. /**
  61. * get role's permissions
  62. * @return Collection
  63. */
  64. public function getPermissions(): Collection
  65. {
  66. return $this->permissions()->get();
  67. }
  68. /**
  69. * get role's departments
  70. * @return Collection
  71. */
  72. public function getDepartments(): Collection
  73. {
  74. return $this->departments()->get();
  75. }
  76. }