diff --git a/src/Laravel/Eloquent/State/CollectionProvider.php b/src/Laravel/Eloquent/State/CollectionProvider.php index 2557dd842c9..3c677520e93 100644 --- a/src/Laravel/Eloquent/State/CollectionProvider.php +++ b/src/Laravel/Eloquent/State/CollectionProvider.php @@ -58,6 +58,29 @@ public function provide(Operation $operation, array $uriVariables = [], array $c $query = $extension->apply($query, $uriVariables, $operation, $context); } + if ($order = $operation->getOrder()) { + $isList = array_is_list($order); + foreach ($order as $property => $direction) { + if ($isList) { + $property = $direction; + $direction = 'ASC'; + } + + if (str_contains($property, '.')) { + [$table, $property] = explode('.', $property); + + // Relation Order by, we need to do laravel eager loading + $query->with([ + $table => fn ($query) => $query->orderBy($property, $direction), + ]); + + continue; + } + + $query->orderBy($property, $direction); + } + } + if (false === $this->pagination->isEnabled($operation, $context)) { return $query->get(); }