From 4c8f5f20f7de259847c88823567a96cca42254db Mon Sep 17 00:00:00 2001 From: Nicolas Demangeat Date: Mon, 16 Jun 2025 09:53:32 +0200 Subject: [PATCH] fix: :bug: add a default behavior in sortQuery method in Controller.php to have the query orderByDesc if no query string --- app/Http/Controllers/Controller.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index 5686abc..5c92252 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -216,13 +216,16 @@ class Controller extends BaseController Session::remove("$rName.sort_col"); Session::remove("$rName.sort_way"); } - if (request()->sort_col and request()->sort_way and Schema::hasColumn($table, request()->sort_col)) { - if ( - !isset($ignore) or - (isset($ignore) and !in_array(request()->sort_col, $ignore)) - ) { - $query = $query->orderBy(\sprintf("$table.%s", request()->sort_col), request()->sort_way); - } + if ( + request()->sort_col && + request()->sort_way && + Schema::hasColumn($table, request()->sort_col) && + (!isset($ignore) || (isset($ignore) && !in_array(request()->sort_col, $ignore))) + ) { + $query = $query->orderBy("$table." . request()->sort_col, request()->sort_way); + } else { + // Else, orderByDesc by default. + $query = $query->orderByDesc("$table.created_at"); } return $query; }