->orderBy('top_date', 'DESC')
return User::where('is_deleted', '0')
->with(['langs', 'categories'])
->where('is_published', true)
->where('subscription', '>=', now())
->orderBy('balls', 'DESC')
->filter($filters)
->paginate(15);
SELECT * FROM users u ORDER BY CASE WHEN u.top_date > NOW() THEN u.top_date END DESC, u.balls DESC
return User::where('is_deleted', '0')
->with(['langs', 'categories'])
->where('is_published', true)
->where('subscription', '>=', now())
->orderByRaw('CASE WHEN top_date > NOW() THEN top_date END DESC, balls DESC')
->filter($filters)
->paginate(15);
$users = User::where('is_deleted', '0')
->with(['langs', 'categories'])
->where('is_published', true)
->where('subscription', '>=', now())
->orderBy('balls', 'DESC')
->filter($filters)
->paginate(15);
foreach ($users as $user) {
if ($user->top_date > Carbon::now()) {
$users = $users->sort(function ($user) {
return $user->top_date;
});
break;
}
}
return $users;
// подготовка запроса
$query = User::where('is_deleted', '0')
->with(['langs', 'categories'])
->where('is_published', true)
->where('subscription', '>=', now())
->orderBy('balls', 'DESC');
// Проверка на условие
if ($query->where('top_date', '>=', now())->count()) - будет лишний запрос в базу и если не 0 то добавляем к запросу сортировку.
{$query = $query->orderBy('top_date', 'DESC');}
// выполнение Запроса.
$query = $query->filter($filters)
->paginate(15);