fix: User 모델 tenant 관계 수정 (user_tenants pivot 사용)
- User 모델: tenants() belongsToMany 관계 추가
- UserService: whereHas('tenants') pivot 필터링으로 변경
- 사용자 생성 시 user_tenants pivot 처리
- tenant_id 컬럼 참조 제거 (다대다 관계)
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
@@ -62,10 +63,27 @@ protected function casts(): array
|
||||
}
|
||||
|
||||
/**
|
||||
* 관계: 테넌트
|
||||
* 관계: 테넌트들 (Many-to-Many via user_tenants)
|
||||
*/
|
||||
public function tenant()
|
||||
public function tenants(): BelongsToMany
|
||||
{
|
||||
return $this->belongsTo(\App\Models\Tenants\Tenant::class, 'tenant_id');
|
||||
return $this->belongsToMany(\App\Models\Tenants\Tenant::class, 'user_tenants')
|
||||
->withTimestamps()
|
||||
->withPivot(['is_active', 'is_default', 'joined_at', 'left_at']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 현재 선택된 테넌트 (세션 기반)
|
||||
*/
|
||||
public function currentTenant()
|
||||
{
|
||||
$tenantId = session('selected_tenant_id');
|
||||
|
||||
if (! $tenantId) {
|
||||
return $this->tenants()->where('is_default', true)->first()
|
||||
?? $this->tenants()->first();
|
||||
}
|
||||
|
||||
return $this->tenants()->find($tenantId);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user