| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- <?php
- namespace App\Models;
- use App\Constants\Util;
- class Menu extends BaseModel
- {
- protected $table = 'menus';
- protected $fillable = ['parent_id', 'title', 'icon' ,'uri' ,'permission_name' ,'sort' ,'status' ,'type' ];
- const TYPE_MENU = 1;
- const TYPE_BUTTON = 2;
- const STATUS_SHOW = 1;
- const STATUS_HIDE = 2;
- public static function isImageIcon(string $value): bool
- {
- $value = trim($value);
- if ($value === '') return true;
- if (preg_match('#^https?://#i', $value)) {
- return filter_var($value, FILTER_VALIDATE_URL) !== false;
- }
- return preg_match('#^/(?!/)\S+$#u', $value) === 1;
- }
- protected function getIconAttribute($value): string
- {
- $value = trim((string) $value);
- return self::isImageIcon($value) ? (string) Util::ensureUrl($value) : '';
- }
- protected function setIconAttribute($value): void
- {
- $value = trim((string) $value);
- $baseUrl = rtrim((string) config('app.url', ''), '/');
- if ($baseUrl !== '' && str_starts_with($value, $baseUrl . '/')) {
- $value = substr($value, strlen($baseUrl));
- }
- $this->attributes['icon'] = $value;
- }
- public function children()
- {
- return $this->hasMany(Menu::class, 'parent_id');
- }
- public function parent()
- {
- return $this->belongsTo(Menu::class, 'parent_id');
- }
- }
|