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

@@ -92,10 +92,17 @@ class User extends Authenticatable implements HasMedia
public function getFormattedCreatedAtAttribute($value)
{
$company_id = (CompanySetting::where('company_id', request()->header('company'))->exists())
? request()->header('company')
: $this->companies()->first()->id;
$dateFormat = CompanySetting::getSetting('carbon_date_format', $company_id);
$companyId = request()->header('company');
if (! $companyId || ! CompanySetting::where('company_id', $companyId)->exists()) {
$firstCompany = $this->companies()->first();
if (! $firstCompany) {
return Carbon::parse($this->created_at)->format('Y-m-d');
}
$companyId = $firstCompany->id;
}
$dateFormat = CompanySetting::getSetting('carbon_date_format', $companyId);
return Carbon::parse($this->created_at)->format($dateFormat);
}