From e9ca94f089af8dec2078daee0e20266a6c27c75e Mon Sep 17 00:00:00 2001 From: Witel Group Date: Mon, 23 Dec 2024 07:50:36 +0000 Subject: [PATCH] change datatable to accept multisort --- app/Services/DataTable/DataTableInput.php | 13 +++++++++---- app/Services/DataTable/DataTableService.php | 5 ++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/app/Services/DataTable/DataTableInput.php b/app/Services/DataTable/DataTableInput.php index 69172f9a..db0cfa7f 100644 --- a/app/Services/DataTable/DataTableInput.php +++ b/app/Services/DataTable/DataTableInput.php @@ -57,12 +57,17 @@ class DataTableInput } /** - * @return Sort|null + * @return array|null */ - public function getSorting(): ?Sort + public function getSorting(): ?array { - return !empty($this->sorting) ? - new Sort($this->sorting[0]->id, $this->sorting[0]->desc, $this->allowedSortings) : null; + $sorts = []; + if (!empty($this->sorting)){ + foreach ($this->sorting as $sort) { + $sorts[] = new Sort($sort->id, $sort->desc, $this->allowedSortings); + } + } + return $sorts == [] ? null : $sorts; } public function getRelations(): array diff --git a/app/Services/DataTable/DataTableService.php b/app/Services/DataTable/DataTableService.php index f8b54d09..af0d0451 100644 --- a/app/Services/DataTable/DataTableService.php +++ b/app/Services/DataTable/DataTableService.php @@ -83,7 +83,10 @@ class DataTableService } $sorting = $this->dataTableInput->getSorting(); - $query = (new ApplySort($query, $sorting))->apply(); + foreach ($sorting as $sort){ + $query = (new ApplySort($query, $sort))->apply(); + } + return $query; }