Menu.php 3.5 KB

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