Files
sam-api/app/Models/Commons/GlobalMenu.php
hskwon d9348c0714 feat: global_menus 테이블 분리 및 모델 구현
- global_menus 테이블 생성 (ID 1번부터 시작)
- 기존 menus(tenant_id IS NULL) → global_menus 데이터 이전
- GlobalMenu 모델 생성
- Menu.globalMenu() 관계를 GlobalMenu 모델로 변경
2025-12-02 20:43:29 +09:00

150 lines
3.3 KiB
PHP

<?php
namespace App\Models\Commons;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* 글로벌 메뉴 모델
*
* 시스템 전체에서 사용하는 기본 메뉴 구조를 정의합니다.
* 테넌트 메뉴(menus)는 이 글로벌 메뉴를 기반으로 복제되어 커스터마이징됩니다.
*
* @property int $id
* @property int|null $parent_id
* @property string $name
* @property string|null $url
* @property string|null $icon
* @property int $sort_order
* @property bool $is_active
* @property bool $hidden
* @property bool $is_external
* @property string|null $external_url
* @property \Carbon\Carbon|null $created_at
* @property \Carbon\Carbon|null $updated_at
* @property \Carbon\Carbon|null $deleted_at
*/
class GlobalMenu extends Model
{
use SoftDeletes;
protected $table = 'global_menus';
protected $fillable = [
'parent_id',
'name',
'url',
'icon',
'sort_order',
'is_active',
'hidden',
'is_external',
'external_url',
];
protected $casts = [
'is_active' => 'boolean',
'hidden' => 'boolean',
'is_external' => 'boolean',
];
/**
* 동기화 비교 대상 필드
*/
public static function getSyncFields(): array
{
return ['name', 'url', 'icon', 'sort_order', 'is_active', 'hidden', 'is_external', 'external_url'];
}
/**
* 상위 메뉴
*/
public function parent(): BelongsTo
{
return $this->belongsTo(GlobalMenu::class, 'parent_id');
}
/**
* 하위 메뉴 목록
*/
public function children(): HasMany
{
return $this->hasMany(GlobalMenu::class, 'parent_id');
}
/**
* 이 글로벌 메뉴로부터 복제된 테넌트 메뉴 목록
*/
public function tenantMenus(): HasMany
{
return $this->hasMany(Menu::class, 'global_menu_id');
}
/**
* 활성화된 메뉴만 조회
*/
public function scopeActive($query)
{
return $query->where('is_active', true);
}
/**
* 숨겨지지 않은 메뉴만 조회
*/
public function scopeVisible($query)
{
return $query->where('hidden', false);
}
/**
* 최상위 메뉴만 조회
*/
public function scopeRoots($query)
{
return $query->whereNull('parent_id');
}
/**
* 정렬된 메뉴 조회
*/
public function scopeOrdered($query)
{
return $query->orderBy('sort_order')->orderBy('id');
}
/**
* 메뉴 레벨 계산 (대메뉴=1, 중메뉴=2, 소메뉴=3)
*/
public function getLevel(): int
{
if (is_null($this->parent_id)) {
return 1;
}
$parent = $this->parent;
if ($parent && is_null($parent->parent_id)) {
return 2;
}
return 3;
}
/**
* 계층 구조로 정렬된 전체 메뉴 트리 조회
*/
public static function getMenuTree(): array
{
$menus = static::with('children.children')
->whereNull('parent_id')
->active()
->visible()
->ordered()
->get();
return $menus->toArray();
}
}