Menu.php 3.2 KB

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