Commit 60ff1e86 authored by Mahmoud Aglan's avatar Mahmoud Aglan

Fix dashboard crash: trainer_compensations table name + graceful fallback

- Add explicit $table = 'trainer_compensations' to TrainerCompensation
  model (Laravel resolved singular 'trainer_compensation' by default)
- Wrap TrainerDuesWidget query in try/catch so dashboard loads even if
  the compensation migration hasn't been run yet
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 19e94971
......@@ -15,6 +15,8 @@ class TrainerCompensation extends Model
{
use BelongsToAcademy, HasUuid, SoftDeletes;
protected $table = 'trainer_compensations';
protected $fillable = [
'academy_id',
'trainer_id',
......
......@@ -12,6 +12,11 @@ class TrainerDuesWidget extends Component
{
public function render()
{
$totalDues = 0;
$trainerTotals = [];
$topTrainers = collect();
try {
$startOfMonth = now()->startOfMonth()->toDateString();
$endOfMonth = now()->endOfMonth()->toDateString();
......@@ -27,8 +32,6 @@ public function render()
->with('employee:id,salary_amount')
->get();
$trainerTotals = [];
foreach ($compensations as $trainerId => $comp) {
$trainerTotals[$trainerId] = ($trainerTotals[$trainerId] ?? 0) + $comp->total_amount;
}
......@@ -40,16 +43,21 @@ public function render()
$totalDues = array_sum($trainerTotals);
if ($trainerTotals) {
$topTrainers = Trainer::whereIn('id', array_keys($trainerTotals))
->with('employee.person:id,name_ar,name')
->with(['employee.person:id,name_ar,name', 'person:id,name_ar,name'])
->get()
->map(fn ($t) => [
'name' => $t->employee?->person?->name_ar ?? $t->trainer_number,
'name' => $t->employee?->person?->name_ar ?? $t->person?->name_ar ?? $t->trainer_number,
'amount' => $trainerTotals[$t->id],
])
->sortByDesc('amount')
->take(5)
->values();
}
} catch (\Illuminate\Database\QueryException $e) {
// Table may not exist yet if migrations haven't run
}
return view('livewire.dashboard.trainer-dues-widget', [
'totalDues' => $totalDues,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment