Handle no-company user in ScopeBouncer middleware and User model

Skip bouncer scoping when user has no companies instead of crashing
on null. Fall back to Y-m-d date format in getFormattedCreatedAtAttribute
when no company settings are available.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Darko Gjorgjijoski
2026-04-04 00:12:00 +02:00
parent c49c130e9e
commit afbc6c1db3
2 changed files with 21 additions and 8 deletions

View File

@@ -32,11 +32,17 @@ class ScopeBouncer
public function handle(Request $request, Closure $next): Response
{
$user = $request->user();
$tenantId = $request->header('company')
? $request->header('company')
: $user->companies()->first()->id;
$company = $request->header('company');
$this->bouncer->scope()->to($tenantId);
if (! $company) {
$firstCompany = $user->companies()->first();
if (! $firstCompany) {
return $next($request);
}
$company = $firstCompany->id;
}
$this->bouncer->scope()->to($company);
return $next($request);
}