fix: 🐛 add a default behavior in sortQuery method in Controller.php to have the query orderByDesc if no query string

This commit is contained in:
Nicolas Demangeat 2025-06-16 09:53:32 +02:00
parent 09d84e31fb
commit 4c8f5f20f7

View file

@ -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;
}