Skip to content

Commit ba0c855

Browse files
toitzisoyuka
andauthored
[Laravel] Fix default order by (#6840)
* fix(laravel): fix default order parameter Closes: #6839 Signed-off-by: Tobias Oitzinger <[email protected]> * Update src/Laravel/Eloquent/State/CollectionProvider.php --------- Signed-off-by: Tobias Oitzinger <[email protected]> Co-authored-by: Antoine Bluchet <[email protected]>
1 parent 61a7f09 commit ba0c855

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/Laravel/Eloquent/State/CollectionProvider.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,29 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
5858
$query = $extension->apply($query, $uriVariables, $operation, $context);
5959
}
6060

61+
if ($order = $operation->getOrder()) {
62+
$isList = array_is_list($order);
63+
foreach ($order as $property => $direction) {
64+
if ($isList) {
65+
$property = $direction;
66+
$direction = 'ASC';
67+
}
68+
69+
if (str_contains($property, '.')) {
70+
[$table, $property] = explode('.', $property);
71+
72+
// Relation Order by, we need to do laravel eager loading
73+
$query->with([
74+
$table => fn ($query) => $query->orderBy($property, $direction),
75+
]);
76+
77+
continue;
78+
}
79+
80+
$query->orderBy($property, $direction);
81+
}
82+
}
83+
6184
if (false === $this->pagination->isEnabled($operation, $context)) {
6285
return $query->get();
6386
}

0 commit comments

Comments
 (0)