I need to create total of debits and credits without creating column for it

Hello, I need help on creating total where debits - credits would be the total. I want to put it under remarks. would it be possible?
No description
Solution:
Did you try ``` ->using(fn() => $query->sum('debits') - $query->sum('credits') ...
Jump to solution
7 Replies
itsmejyv
itsmejyvOP4mo ago
I'm using the built in summarize table
itsmejyv
itsmejyvOP4mo ago
Im so close to it but I'm not sure what logic need to use if I filtered the data using search or filters.
// this work but only overall total, if I use search and filter. it still get the overall total not the current total of debits and credits.

$query = SavingsDeposit::query();

$debitsTotal = $query->sum('debits');
$creditsTotal = $query->sum('credits');
$total = $debitsTotal - $creditsTotal;

Tables\Columns\TextColumn::make('')
->formatStateUsing(fn() => $total)
->summarize(
Summarizer::make()
->label('Total Amount')
->using(fn() => $total)
),
// this work but only overall total, if I use search and filter. it still get the overall total not the current total of debits and credits.

$query = SavingsDeposit::query();

$debitsTotal = $query->sum('debits');
$creditsTotal = $query->sum('credits');
$total = $debitsTotal - $creditsTotal;

Tables\Columns\TextColumn::make('')
->formatStateUsing(fn() => $total)
->summarize(
Summarizer::make()
->label('Total Amount')
->using(fn() => $total)
),
MilenKo
MilenKo4mo ago
I believe to make the column be allowed to be searched, you need to add ->searcheable() and same thing for filters - you need to define the column of your filter to be matching the sumarizer column... I am not an expert and have not yet used summarizer columns to show you an actual example, but it is a common rule for all columns to be filtered or searcheable, so ... Hope it gets you to the end and you can check the TableBuilder docs for the searcheable() method and filters...
Solution
Bruno Pereira
Bruno Pereira4mo ago
Did you try
->using(fn() => $query->sum('debits') - $query->sum('credits')
->using(fn() => $query->sum('debits') - $query->sum('credits')
Bruno Pereira
Bruno Pereira4mo ago
And remove the extra vars because they're probably not refreshing on table refresh
itsmejyv
itsmejyvOP4mo ago
you're right , I define a static query there so thats why its not updating.
->summarize(
Summarizer::make()
->label('Total Amount')
->using(fn ($query) => $query->sum('debits') - $query->sum('credits')) // Calculate dynamically
),
->summarize(
Summarizer::make()
->label('Total Amount')
->using(fn ($query) => $query->sum('debits') - $query->sum('credits')) // Calculate dynamically
),
Want results from more Discord servers?
Add your server