Files
sam-api/app/Models/Commons/Department.php
2025-08-21 09:50:15 +09:00

57 lines
1.6 KiB
PHP

<?php
namespace App\Models\Commons;
use App\Models\Members\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Traits\BelongsToTenant;
use App\Traits\ModelTrait;
/**
* @mixin IdeHelperDepartment
*/
class Department extends Model
{
use SoftDeletes, BelongsToTenant, ModelTrait;
protected $table = 'departments';
protected $fillable = [
'tenant_id','code','name','description','is_active','sort_order',
'created_by','updated_by','deleted_by',
];
protected $casts = [
'tenant_id' => 'integer',
'is_active' => 'integer',
'sort_order'=> 'integer',
];
/** Relations */
public function departmentUsers()
{
return $this->hasMany(DepartmentUser::class, 'department_id');
}
public function users()
{
// User 네임스페이스가 다르면 여기만 맞춰줘.
return $this->belongsToMany(User::class, 'department_user', 'department_id', 'user_id')
->withPivot(['tenant_id','is_primary','joined_at','left_at','created_at','updated_at','deleted_at'])
->withTimestamps();
}
public function departmentPermissions()
{
return $this->hasMany(DepartmentPermission::class, 'department_id');
}
public function permissions()
{
return $this->belongsToMany(\Spatie\Permission\Models\Permission::class, 'department_permissions', 'department_id', 'permission_id')
->withPivot(['tenant_id','menu_id','is_allowed','created_at','updated_at','deleted_at'])
->withTimestamps();
}
}