36 lines
1.1 KiB
PHP
36 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Product;
|
|
use Illuminate\Http\Request;
|
|
|
|
class LeaderboardController extends Controller
|
|
{
|
|
private const PER_PAGE = 10;
|
|
|
|
/**
|
|
* Display the leaderboard: top products by period, or the newest products.
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$tab = $request->query('tab') === 'new' ? 'new' : 'top';
|
|
$period = array_key_exists((string) $request->query('period'), Product::LEADERBOARD_PERIODS)
|
|
? $request->query('period')
|
|
: 'today';
|
|
|
|
$query = Product::query()->with('tags');
|
|
|
|
$products = ($tab === 'new' ? $query->approved()->latest('products.created_at')->latest('products.id') : $query->topFor($period))
|
|
->paginate(self::PER_PAGE)
|
|
->appends($tab === 'new' ? ['tab' => 'new'] : ['tab' => 'top', 'period' => $period]);
|
|
|
|
return view('leaderboard.index', [
|
|
'products' => $products,
|
|
'tab' => $tab,
|
|
'period' => $period,
|
|
'periods' => Product::LEADERBOARD_PERIODS,
|
|
]);
|
|
}
|
|
}
|