Menu.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\Models\System;
  3. use App\Exceptions\ApiException;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Support\Facades\Cache;
  6. use Illuminate\Support\Facades\DB;
  7. use Spatie\Permission\Guard;
  8. use Spatie\Permission\Traits\HasPermissions;
  9. use Symfony\Component\HttpFoundation\Response;
  10. class Menu extends Model
  11. {
  12. // use HasPermissions;
  13. protected $table = 'system_menus';
  14. protected $attributes = [];
  15. public static $snakeAttributes = false;
  16. protected $appends = ['permission'];
  17. protected $casts = [
  18. // 'visible' => 'bool',
  19. // 'keepAlive' => 'bool',
  20. // 'alwaysShow' => 'bool',
  21. // 'parent_id' => 'camel'
  22. ];
  23. public function getPermissionAttribute()
  24. {
  25. $tableNames = config('permission.table_names');
  26. $permission_ids = DB::table($tableNames['model_has_permissions'])->where('model_type', Menu::class)->where('model_id', $this->attributes['id'])->pluck('permission_id');
  27. $permission_names = DB::table($tableNames['permissions'])->whereIn('id', $permission_ids)->pluck('name');
  28. return implode('|', $permission_names->toArray());
  29. }
  30. public function setComponentNameAttribute($value)
  31. {
  32. $this->attributes['component_name'] = $value;
  33. }
  34. public function setVisibleAttribute($value)
  35. {
  36. $this->attributes['visible'] = $value ? 1 : 0;
  37. }
  38. public function setAlwaysShowAttribute($value)
  39. {
  40. $this->attributes['always_show'] = $value ? 1 : 0;
  41. }
  42. // public function getVisibleAttribute($value)
  43. // {
  44. // $this->attributes['visible'] = !$value;
  45. // }
  46. public function setKeepAliveAttribute($value)
  47. {
  48. $this->attributes['keep_alive'] = $value ? 1 : 0;
  49. }
  50. }