Files
coruna-lab/app/Http/Controllers/Concerns/PortalAware.php
T
2026-09-06 03:55:24 +08:00

51 lines
1.1 KiB
PHP

<?php
namespace App\Http\Controllers\Concerns;
use App\Models\User;
trait PortalAware
{
protected function portal(): string
{
$name = request()->route()?->getName() ?? '';
return str_starts_with($name, 'user.') ? 'user' : 'admin';
}
protected function isAgentPortal(): bool
{
return $this->portal() === 'user';
}
protected function agent(): ?User
{
if (! $this->isAgentPortal()) {
return null;
}
/** @var User|null $user */
$user = auth('agent')->user();
return $user;
}
/** @return array{0: string|null, 1: int|null} */
protected function viewerActor(): array
{
if ($this->isAgentPortal()) {
$agent = $this->agent();
return $agent ? ['agent', (int) $agent->id] : [null, null];
}
$admin = auth('admin')->user();
return $admin ? ['admin', (int) $admin->id] : [null, null];
}
protected function routeName(string $suffix): string
{
return $this->portal().'.'.$suffix;
}
}