fix: 🎨 add condition to have invoiceModels sorted by query string or orderByDesc if no query string in InvoiceController
This commit is contained in:
parent
4c8f5f20f7
commit
c63f2b62e9
2 changed files with 5 additions and 4 deletions
|
|
@ -25,8 +25,12 @@ class InvoiceController extends Controller
|
|||
*/
|
||||
public function index(Request $request): \Illuminate\Contracts\View\View
|
||||
{
|
||||
// If no sorting is requested, apply the default orderByDesc.
|
||||
/** @var \Illuminate\Database\Eloquent\Builder $invoiceModels */
|
||||
$invoiceModels = Invoice::query();
|
||||
$invoiceModels = (!request()->query('sort_col') && !request()->query('sort_way'))
|
||||
? Invoice::query()->orderByDesc('created_at')
|
||||
: Invoice::query();
|
||||
|
||||
$status = intval($request->status);
|
||||
$invoiceModels = $invoiceModels->when($status, function ($query) use ($status) {
|
||||
$query->where('status', $status);
|
||||
|
|
|
|||
|
|
@ -223,9 +223,6 @@ class Controller extends BaseController
|
|||
(!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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue