Hello I have a users table with columns
id, username, password
I also have an orders table with columns
id, user_id, price, status
In the index method of the UsersController, I would like to order my users based on the amount of purchases they have made. The purchases of each user can be gotten as the sum of the price of all the orders in which the user_id of the order is equal to the id of a user and in which the order.status of such an order is equal to 4
The restriction in order status is made because users have the possibility of cancelling orders in my system so I'll like to query only the orders which have not been canceled.
Trials
I tried getting the users based on
$users = User::leftJoin('orders', 'users.id', '=', 'orders.user_id')
->where('orders.status', '!=', 2)->where('orders.status', '!=', 5)
->groupBy('orders.user_id');
But yet I still get errors.
Based on my Models, I could get all the purchases of a particular user by usinng a query such as
User::find(1)->orders()->where('orders.status', '=', 4)->sum('orders.price')
Aucun commentaire:
Enregistrer un commentaire