getOwnerRecord(); // Get distinct periods for this budget $periods = \App\Models\Accounting\BudgetAllocation::query() ->join('budget_items', 'budget_allocations.budget_item_id', '=', 'budget_items.id') ->where('budget_items.budget_id', $budget->id) ->orderBy('start_date') ->pluck('period') ->unique() ->values() ->toArray(); return $table ->recordTitleAttribute('account_id') ->paginated(false) ->modifyQueryUsing( fn ($query) => $query->with(['account', 'allocations']) ) ->columns(array_merge([ TextColumn::make('account.name') ->label('Accounts') ->sortable() ->searchable(), ], collect($periods)->map( fn ($period) => TextInputColumn::make("allocations_by_period.{$period}") ->label($period) ->getStateUsing( fn ($record) => $record->allocations->firstWhere('period', $period)?->amount ) ->updateStateUsing(function (BudgetItem $record, $state) use ($period) { $allocation = $record->allocations->firstWhere('period', $period); if ($allocation) { $allocation->update(['amount' => $state]); } }) )->all())); } }