assuming i have a table
Orders
with margins
id, userId, quantity, description
and the table
user
with various fields
as if I earned to receive all users (with all its fields), as well as the amount of the column "sum" of orders associated with this user?
assuming i have:
User: {ID: 15, Firstname: jim, LastName: Morrison, gender: male}
and
order: {id: 1, userId: 15, amount: 10, description: "order xxx"},
order: {id: 3, userId: 15, amount: 40, description: "order yyy"}
I would like to receive:
User: {ID: 15, Firstname: jim, LastName: Morrison, gender: male, orderAmount: 50}
Of course, I would like to avoid the foreach statement.
I installed this in my user model
public function userOrder (){ return $this->hasMany('Order', 'userId'); }
And I tried this:
return $this->hasMany('Order', 'userId')->sum('amount');
no luck ...