Sum (quantity * price)
Hello,
Tried for hours to get a total for orders , there's something I'm doing wrong?
or trough an attribute
25 Replies
So in that you are expecting the tital to come from the items class. I would suggest you have a relationship that returns itemsTotal within the record model. Otherwise use formatState using from items and loop through i.e.
As a rough example.
Thank you, it's working, how can I just access the number returned?
$record->total = [{"SUM(quantity * price)":"162.00"}]
Adjust it to
It gives me that : App\Models\Invoice::total must return a relationship instance.
->content(fn ($record) => $record->items()->sum(DB::raw('quantity * price'))
Thanks, it was my first try without the parentheses...
How can I use that for a Table now?
uhhhhh
using a subselect on the query
by extending getTableQuery()
It's possible to use something like this for table and form?
they are different problems
in the form, you only need to do it once
in the table, it needs to be added to the table query otherwise you will experience an n+1 problem
hence why i suggested adding a subselect statement onto the table query. have you done that in SQL before?
IN (SELECT FROM...) these are the subselects I have used before but just a few times.
Added this to InvoiceRessource\Pages\ListInvoices.php
Could not check compatibility between App\Filament\Resources\InvoiceResource\Pages\ListInvoices::getTableQuery():
https://reinink.ca/articles/dynamic-relationships-in-laravel-using-subqueries#introducing-subqueries
Dynamic relationships in Laravel using subqueries
In this article I explain how to use subqueries to create dynamic relationships in Laravel. This is a powerful technique that allows you to push more work into the database layer of your app. This can have a huge impact on performance by allowing you to drastically reduce the number of database queries executed and overall memory used.
i would write it for you but i want you to learn so i dont have to do it again 😂
I know and you're right!
Finally!
The compatibility problem was solved by :
use Illuminate\Database\Eloquent\Builder;
Sorry just two little more things :
How can I refresh the Total when I add a product?
Why is it adding the shipping costs 2 times?
you need a livewire event to trigger from the relation manager
and listen on the page and call $refresh
Filament
Update Main form after Relationship Manager CRUD by iotron - Tricks...
Filament is a collection of tools for rapidly building beautiful TALL stack apps, designed for humans.
yup
any clue on why is it adding shipping 2 times?
i do not, you should debug that using dd()
ok thank you
Sorry for needing your help again, but how can I get the total of all invoices with this :
Remove the whereColumn
$total->big_total = Property [big_total] does not exist on the Eloquent builder instance.
How can I access big_total?
Ahh my bad. The where was right, I missed the sub select. Why not just do:
$invoices = Invoice::query()
->addSelect([
'pk_invoices.id',
'big_total' => InvoiceItem::selectRaw('SUM(quantity * price)')
->whereColumn('invoice_id', 'pk_invoices.id')
])
->get();
$total = $invoices->sum('big_total');
Thank you lets try this 🙂
It's working now, big thanks for your help.
No problem