Menu.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace Dcat\Admin\Models;
  3. use Dcat\Admin\Traits\ModelTree;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  6. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  7. use Illuminate\Database\Eloquent\Relations\HasOne;
  8. use Illuminate\Support\Facades\DB;
  9. /**
  10. * Class Menu.
  11. *
  12. * @property int $id
  13. *
  14. * @method where($parent_id, $id)
  15. */
  16. class Menu extends Model
  17. {
  18. use MenuCache,
  19. ModelTree {
  20. ModelTree::boot as treeBoot;
  21. }
  22. /**
  23. * The attributes that are mass assignable.
  24. *
  25. * @var array
  26. */
  27. protected $fillable = ['parent_id', 'order', 'title', 'icon', 'uri', 'permission_id'];
  28. /**
  29. * Create a new Eloquent model instance.
  30. *
  31. * @param array $attributes
  32. */
  33. public function __construct(array $attributes = [])
  34. {
  35. $connection = config('admin.database.connection') ?: config('database.default');
  36. $this->setConnection($connection);
  37. $this->setTable(config('admin.database.menu_table'));
  38. parent::__construct($attributes);
  39. }
  40. /**
  41. * A Menu belongs to many roles.
  42. *
  43. * @return BelongsToMany
  44. */
  45. public function roles() : BelongsToMany
  46. {
  47. $pivotTable = config('admin.database.role_menu_table');
  48. $relatedModel = config('admin.database.roles_model');
  49. return $this->belongsToMany($relatedModel, $pivotTable, 'menu_id', 'role_id');
  50. }
  51. public function permissions(): BelongsToMany
  52. {
  53. $pivotTable = config('admin.database.permission_menu_table');
  54. $relatedModel = config('admin.database.permissions_model');
  55. return $this->belongsToMany($relatedModel, $pivotTable, 'menu_id', 'permission_id');
  56. }
  57. /**
  58. * @param bool $force
  59. *
  60. * @return array
  61. */
  62. public function allNodes(bool $force = false): array
  63. {
  64. if ($force || $this->queryCallback instanceof \Closure) {
  65. return $this->fetchAll();
  66. }
  67. return $this->remember(function () {
  68. return $this->fetchAll();
  69. });
  70. }
  71. /**
  72. * @return array
  73. */
  74. protected function fetchAll(): array
  75. {
  76. $connection = config('admin.database.connection') ?: config('database.default');
  77. $orderColumn = DB::connection($connection)->getQueryGrammar()->wrap($this->orderColumn);
  78. $byOrder = 'ROOT ASC, '.$orderColumn;
  79. $self = new static;
  80. if ($this->queryCallback instanceof \Closure) {
  81. $self = call_user_func($this->queryCallback, $self);
  82. }
  83. if (static::withPermission()) {
  84. $self = $self->with('permissions');
  85. }
  86. return $self->with('roles')
  87. ->selectRaw('*, '.$orderColumn.' ROOT')
  88. ->orderByRaw($byOrder)
  89. ->get()
  90. ->toArray();
  91. }
  92. /**
  93. * determine if enable menu bind permission.
  94. *
  95. * @return bool
  96. */
  97. public static function withPermission()
  98. {
  99. return (bool) config('admin.menu.bind_permission');
  100. }
  101. /**
  102. * Detach models from the relationship.
  103. *
  104. * @return void
  105. */
  106. protected static function boot()
  107. {
  108. static::treeBoot();
  109. static::deleting(function ($model) {
  110. $model->roles()->detach();
  111. $model->permissions()->detach();
  112. $model->destroyCache();
  113. });
  114. static::saved(function ($model) {
  115. $model->destroyCache();
  116. });
  117. }
  118. }