2025-07-17 10:05:47 +09:00
|
|
|
<?php
|
|
|
|
|
|
2025-07-29 16:04:28 +09:00
|
|
|
namespace App\Models\Members;
|
2025-07-17 10:05:47 +09:00
|
|
|
|
2025-07-29 13:00:25 +09:00
|
|
|
use App\Models\Commons\Role;
|
2025-08-14 00:55:08 +09:00
|
|
|
use App\Models\Commons\File;
|
|
|
|
|
use App\Models\Tenants\Tenant;
|
2025-07-29 13:00:25 +09:00
|
|
|
use App\Traits\ModelTrait;
|
2025-07-26 14:23:13 +09:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2025-07-17 10:05:47 +09:00
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
|
|
|
use Illuminate\Notifications\Notifiable;
|
|
|
|
|
use Laravel\Sanctum\HasApiTokens;
|
2025-08-14 00:55:08 +09:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
2025-07-17 10:05:47 +09:00
|
|
|
|
|
|
|
|
class User extends Authenticatable
|
|
|
|
|
{
|
2025-07-28 12:57:08 +09:00
|
|
|
use HasApiTokens, Notifiable, SoftDeletes, ModelTrait;
|
2025-07-26 14:23:13 +09:00
|
|
|
|
2025-08-14 00:55:08 +09:00
|
|
|
protected $fillable = [
|
|
|
|
|
'name',
|
|
|
|
|
'phone',
|
|
|
|
|
'email',
|
|
|
|
|
'options',
|
|
|
|
|
'profile_photo_path',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $guarded = [
|
|
|
|
|
'id',
|
|
|
|
|
'user_id',
|
|
|
|
|
'password',
|
|
|
|
|
'remember_token',
|
|
|
|
|
'two_factor_secret',
|
|
|
|
|
'two_factor_recovery_codes',
|
|
|
|
|
'two_factor_confirmed_at',
|
|
|
|
|
'email_verified_at',
|
|
|
|
|
'last_login_at',
|
|
|
|
|
'current_team_id',
|
|
|
|
|
'deleted_at',
|
|
|
|
|
'created_at',
|
|
|
|
|
'updated_at',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $casts = [
|
|
|
|
|
'email_verified_at' => 'datetime',
|
|
|
|
|
'last_login_at' => 'datetime',
|
|
|
|
|
'options' => 'array',
|
|
|
|
|
'deleted_at' => 'datetime',
|
|
|
|
|
];
|
|
|
|
|
|
2025-07-26 14:23:13 +09:00
|
|
|
protected $hidden = [
|
|
|
|
|
'password', 'remember_token',
|
2025-08-14 17:20:28 +09:00
|
|
|
'two_factor_secret', 'two_factor_recovery_codes', 'two_factor_confirmed_at',
|
|
|
|
|
'deleted_at',
|
2025-07-26 14:23:13 +09:00
|
|
|
];
|
2025-07-17 10:05:47 +09:00
|
|
|
|
2025-07-26 01:23:02 +09:00
|
|
|
public function userTenants()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(UserTenant::class);
|
|
|
|
|
}
|
2025-07-17 10:05:47 +09:00
|
|
|
|
2025-08-14 00:55:08 +09:00
|
|
|
public function userTenant()
|
2025-07-26 01:23:02 +09:00
|
|
|
{
|
2025-07-26 14:23:13 +09:00
|
|
|
return $this->hasOne(UserTenant::class)->where('is_active', 1);
|
2025-07-26 01:23:02 +09:00
|
|
|
}
|
2025-07-17 10:05:47 +09:00
|
|
|
|
2025-07-26 01:23:02 +09:00
|
|
|
public function userRoles()
|
2025-07-17 10:05:47 +09:00
|
|
|
{
|
2025-07-26 01:23:02 +09:00
|
|
|
return $this->hasMany(UserRole::class);
|
2025-07-17 10:05:47 +09:00
|
|
|
}
|
|
|
|
|
|
2025-07-26 01:23:02 +09:00
|
|
|
public function roles()
|
2025-07-17 10:05:47 +09:00
|
|
|
{
|
2025-08-14 00:55:08 +09:00
|
|
|
return $this->belongsToMany(Role::class, 'user_roles')->withPivot('tenant_id', 'assigned_at');
|
2025-07-17 10:05:47 +09:00
|
|
|
}
|
2025-07-26 14:23:13 +09:00
|
|
|
|
|
|
|
|
public function userTenantById($tenantId)
|
|
|
|
|
{
|
|
|
|
|
return $this->hasOne(UserTenant::class)->where('tenant_id', $tenantId);
|
|
|
|
|
}
|
2025-07-29 13:00:25 +09:00
|
|
|
|
|
|
|
|
public function files()
|
|
|
|
|
{
|
|
|
|
|
return $this->morphMany(File::class, 'fileable');
|
|
|
|
|
}
|
2025-08-14 00:55:08 +09:00
|
|
|
|
|
|
|
|
public function tenantsMembership(): BelongsToMany
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(Tenant::class, 'user_tenants', 'user_id', 'tenant_id')
|
|
|
|
|
->as('membership') // pivot 대신 membership으로 표기
|
|
|
|
|
->withPivot(['is_active', 'is_default', 'joined_at', 'left_at', 'deleted_at'])
|
|
|
|
|
->wherePivotNull('deleted_at'); // 소프트삭제 제외
|
|
|
|
|
}
|
2025-07-17 10:05:47 +09:00
|
|
|
}
|