Files
sam-manage/app/Models/Department.php
hskwon d4534f0d3f feat(mng): 사용자 역할/부서 매핑 및 부서관리 복구/영구삭제 기능 추가
사용자 관리:
- 사용자 등록/수정 시 테넌트별 역할/부서 선택 기능 추가
- Department, UserRole, DepartmentUser 모델 추가
- User 모델에 역할/부서 관계 및 헬퍼 메서드 추가
- syncRoles/syncDepartments 메서드 (forceDelete로 유니크 키 충돌 방지)
- 체크박스 UI로 다중 선택 지원

부서 관리:
- Soft Delete 필터 (정상만/전체/삭제된 항목만)
- 복구(restore) 및 영구삭제(forceDelete) 기능 추가
- Department 모델에 SoftDeletes 트레이트 추가
- 삭제된 항목 빨간 배경 + "삭제됨" 배지 표시
2025-11-26 20:28:07 +09:00

75 lines
1.6 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
class Department extends Model
{
use SoftDeletes;
protected $table = 'departments';
protected $fillable = [
'tenant_id',
'parent_id',
'code',
'name',
'description',
'is_active',
'sort_order',
'created_by',
'updated_by',
'deleted_by',
];
protected $casts = [
'tenant_id' => 'integer',
'parent_id' => 'integer',
'is_active' => 'boolean',
'sort_order' => 'integer',
];
protected $hidden = [
'deleted_by',
'deleted_at',
];
/**
* 상위 부서
*/
public function parent(): BelongsTo
{
return $this->belongsTo(self::class, 'parent_id');
}
/**
* 하위 부서
*/
public function children(): HasMany
{
return $this->hasMany(self::class, 'parent_id');
}
/**
* 부서-사용자 매핑 (DepartmentUser pivot)
*/
public function departmentUsers(): HasMany
{
return $this->hasMany(DepartmentUser::class, 'department_id');
}
/**
* 부서에 속한 사용자들 (belongsToMany)
*/
public function users()
{
return $this->belongsToMany(User::class, 'department_user')
->withTimestamps()
->withPivot(['tenant_id', 'is_primary', 'joined_at', 'left_at']);
}
}