-1

so I have 2 tables, products and sales.

Product is ABC, and in the sales A = 16, B= 18, C=10.

and I want to show the products ordered by best selling so the order would be like BAC, how to do that?

thank you for your help :)


Sorry guys if I wasnt clear enough,so here is my complete scenario

Products table

milk
egg
chocolate 

Sales table

milk
milk
egg
milk
chocolate
egg
chocolate
chocolate
milk

and then in the homepage I wanna show products order by best selling, so the order would be

milk
chocolate
egg

I still dont have any query as I still confused how to do that

reeouw
  • 35
  • 6

1 Answers1

0

You did not provide the tables schema but if it is a belongsTo relationship

class Product extends Model
{
   public function sale()
   {
      return $this->belongsTo(App\Models\Sale::class,'sale_id')
   }
}

Then

$products = Product::with('sale')->orderBy('sale.count')->get();
Mahdi Younesi
  • 6,889
  • 2
  • 20
  • 51