#Table with Records (no Query) crashes while using summarize

12 messages · Page 1 of 1 (latest)

civic plank
#

Im using a table with static records and want to show a sum at the end of the table with the summarize method.

Here is my code:

return $table
    ->records(fn (self $livewire) => $livewire->data)
    ->selectable()
    ->columns([
        TextColumn::make('name'),
        TextColumn::make('amount')
            ->label('Betrag')
            ->money()
            ->color('primary')
            ->summarize(Sum::make()),
    ])

Here the error i get:
getTableSummarySelectedState(): Argument #1 ($query) must be of type Illuminate\Database\Eloquent\Builder, null given

The function needs a query instance to work but apparently there is none:

    /**
     * @return array<string, mixed>
     */
    public function getTableSummarySelectedState(Builder $query, ?Closure $modifyQueryUsing = null): array
civic plank
#

Kind of a fix to give the table an empty query...it works but there should be a better way.

cursive raft
#

How do you summarise static data? Last time I looked it was not supported.

civic plank
#

Oh okay i didnt know it was not supported thats good to know because i thought its just a bug or a scenario nobody thought about.

Everybody should know that my "fix" is nowhere best practice but it works for me for now.
Maybe in the future when i have time i could try to make a pr but right now i dont have the time.

return $table
    ->query(MnxEvent::query())
    ->records(fn (self $livewire) => $livewire->data)
    ->selectable()
    ->currentSelectionLivewireProperty('selectedEventAdvances')
    ->liveEntangleSelection() // this is my own patch to get live updates on select
    ->columns([
        TextColumn::make('name'),
        TextColumn::make('amount')
            ->label('Abschlagsbetrag')
            ->money()
            ->color('primary')
            ->summarize(Summarizer::make('sum')
                ->money()
                ->using(function (self $livewire) {
                    return collect($livewire->data)->filter(fn ($item) => in_array($item['id'], $livewire->selectedEventAdvances))->sum('amount');
                })
            ),
    ])

You can also see ive implemented my own "liveEntangleSelection" with a quick composer patch.
That functionality would be nice in the future aswell (maybe another pr of me when i got time).
I use the live entanglement to show a sum for all selected entries which was an requirement of our customer.

cursive raft
#

@oblique sinew I don't think we support summarisers on custom data (at least we don't mention it in the docs), but looks like it's not too hard to add with custom Summarizers. Should this work?

oblique sinew
#

have you checked if theres an issue open for it

cursive raft
#

Nope, I considered this as "not supported" for now, so I didn't look for bugs 😅

civic plank
#

Ah i missed that ticket - sorry

#

FYI: my empty query i added is not running at all so its just there to fix the error

#

I will drop my quick fix in there for now so people can use it for now