One option:
You can edit the existing relationship or create a new relationship for Cart in the User model to join the products table and do a calculation to get the total price.
//User model
public function cartsWithProductPrice(){
return $this->hasMany(Cart::class)
->join('products', 'carts.product_id', 'products.id')
->select('carts.*',
DB::raw('carts.quantity * products.price as price')
);
}
And when you call for example User::with('cartsWithProductPrice')->find(1);
it should give you the cart relationship with a field named price
with total price.
Hope that this will point you in the right direction.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…