Summarize Sum of a calculated field

Hi! I'm trying to show the sum of a calculated attribute.
I have a Transaction model which has many Products. The pivot table has the quantity and the unit price for the product.

So i have the attribute amount which is calculated and appended to the transaction.
protected function amount(): Attribute
    {
        return Attribute::make(
            get: function () {
                return $this?->products->map(
                    fn ($product) => $product->pivot->quantity * $product->pivot->unit_price
                )->sum();
            },
        );
    }

I can display the amount on my table with TextColumn('amount'), but when i try to add the summary i get a error that transactions.amount is not a field in the DB.
So my question is how do i make it use the eloquent model instead of a DB query?
Was this page helpful?