jeudi 21 février 2019

Ordering by foreign key field count

I have three tables a product, user and purchases table. I am trying to return a list of the category of products that the user has most frequently purchased. So the list should look like : Garden 4, Food 3, Sport 2 etc.

Here are my tables:

products: id, name, category, price users: id, name purchases: id, users_id, products_id

I am struggling to work out how to do this, this is what i have so far

Purchase::with('products')->where('purchases.users_id', '=', Auth::users->id())



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire