我一直在开发一个购物车应用程序,现在我来讨论以下问题..
There is a User, a Product and a Cart object.
- Cart表只包含以下列:
id
、user_id
、product_id
和时间戳. - UserModel
hasMany
购物车(因为一个用户可以存储多个产品). - CartModel
belongsTo
a用户和CartModelhasMany
产品.
现在来计算一下产品总数,我只需打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).