I've been working on a shopping cart application and now I've come to the following issue..
There is a User, a Product and a Cart object.
- The Cart table only contains the following columns:
id
, user_id
, product_id
and timestamps.
- The UserModel
hasMany
Carts (because a user can store multiple products).
- The CartModel
belongsTo
a User and CartModel hasMany
Products.
Now to calculate the total products I can just call: Auth::user()->cart()->count()
.
My question is: How can I get the SUM()
of prices (a column of product) of the products in cart by this User?
I would like to accomplish this with Eloquent and not by using a query (mainly because I believe it is a lot cleaner).
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…