Provolkata
Provolkata

Reputation: 33

Laravel 5.1 display best selling products

I am learning laravel 5.1 at the moment and I just want to know how to go about displaying lets say the top 3 best sold products. Currently I can display all products.

@foreach($products as $product)
            <div class="inline">
            <img src="{{ asset($product->image) }}" height="150" width="100"/>
            <a href="{{route('product.show', $product->id)}}">{{ $product->name }}</a>
            <div class="bold2">£{{ $product->price }}</div>         
            </div>
@endforeach

I have a row in my database for each product called 'sale' and contains some random numbers for each product. p.s. I'm making this just to learn, it's not a real website.

Upvotes: 0

Views: 1639

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163788

If you are asking about working with DB, then you should learn Eloquent and probably Query Builder.

If you're getting all products with something like this:

$products = Product::all();

Then to get 3 best selling products, you'll need to use something like this:

$products = Product::orderBy('sell', 'desc')->take(3)->get();

Upvotes: 1

Related Questions