@@ -188,17 +188,37 @@ public function get(): Collection
188
188
{
189
189
return $ this ->query ()->get ();
190
190
}
191
+
192
+ /**
193
+ * Apply order to query by sort options.
194
+ *
195
+ * @param Builder $query Query builder
196
+ * @param SortOptions|SortOptions[] $sortOptions How list of items should be sorted
197
+ *
198
+ * @return Builder
199
+ */
200
+ private function applyOrderBy (Builder $ query , $ sortOptions ): Builder
201
+ {
202
+ if ($ sortOptions instanceof SortOptions) {
203
+ $ query ->orderBy ($ sortOptions ->orderBy , $ sortOptions ->sortOrder );
204
+ } elseif (is_array ($ sortOptions )) {
205
+ foreach ($ sortOptions as $ sortOption ) {
206
+ $ query ->orderBy ($ sortOption ->orderBy , $ sortOption ->sortOrder );
207
+ }
208
+ }
209
+ return $ query ;
210
+ }
191
211
192
212
/** {@inheritdoc} */
193
213
public function getPage (
194
214
PagingInfo $ paging ,
195
215
array $ fieldValues = [],
196
- ? SortOptions $ sortOptions = null
216
+ $ sortOptions = null
197
217
): LengthAwarePaginator {
198
218
$ builder = $ this
199
219
->query ()
200
220
->when ($ sortOptions , function (Builder $ query ) use ($ sortOptions ) {
201
- return $ query -> orderBy ( $ sortOptions -> orderBy , $ sortOptions-> sortOrder );
221
+ return $ this -> applyOrderBy ( $ query , $ sortOptions );
202
222
});
203
223
$ builder ->addNestedWhereQuery ($ this ->getNestedWhereConditions ($ builder ->getQuery (), $ fieldValues ));
204
224
@@ -209,12 +229,12 @@ public function getPage(
209
229
public function getCursorPage (
210
230
CursorRequest $ cursor ,
211
231
array $ fieldValues = [],
212
- ? SortOptions $ sortOptions = null
232
+ $ sortOptions = null
213
233
): CursorResult {
214
234
$ builder = $ this
215
235
->query ()
216
236
->when ($ sortOptions , function (Builder $ query ) use ($ sortOptions ) {
217
- return $ query -> orderBy ( $ sortOptions -> orderBy , $ sortOptions-> sortOrder );
237
+ return $ this -> applyOrderBy ( $ query , $ sortOptions );
218
238
});
219
239
$ builder ->addNestedWhereQuery ($ this ->getNestedWhereConditions ($ builder ->getQuery (), $ fieldValues ));
220
240
@@ -337,7 +357,7 @@ private function performJoin($query, string $table, string $foreign, string $oth
337
357
* @param array $with Which relations should be preloaded
338
358
* @param array|null $withCounts Which related entities should be counted
339
359
* @param array|null $where Conditions that retrieved entities should satisfy
340
- * @param SortOptions|null $sortOptions How list of item should be sorted
360
+ * @param SortOptions|SortOptions[]| null $sortOptions How list of item should be sorted
341
361
*
342
362
* @return Builder
343
363
*
@@ -347,7 +367,7 @@ protected function getWithBuilder(
347
367
array $ with ,
348
368
?array $ withCounts = null ,
349
369
?array $ where = null ,
350
- ? SortOptions $ sortOptions = null
370
+ $ sortOptions = null
351
371
): Builder {
352
372
return $ this ->query ()
353
373
->when ($ with , function (Builder $ query ) use ($ with ) {
@@ -360,7 +380,7 @@ protected function getWithBuilder(
360
380
return $ query ->where ($ where );
361
381
})
362
382
->when ($ sortOptions , function (Builder $ query ) use ($ sortOptions ) {
363
- return $ query -> orderBy ( $ sortOptions -> orderBy , $ sortOptions-> sortOrder );
383
+ return $ this -> applyOrderBy ( $ query , $ sortOptions );
364
384
});
365
385
}
366
386
@@ -390,15 +410,15 @@ public function getWith(
390
410
array $ with ,
391
411
?array $ withCounts = null ,
392
412
?array $ where = null ,
393
- ? SortOptions $ sortOptions = null
413
+ $ sortOptions = null
394
414
): Collection {
395
415
$ builder = $ this ->query ()
396
416
->with ($ with )
397
417
->when ($ withCounts , function (Builder $ query ) use ($ withCounts ) {
398
418
return $ query ->withCount ($ withCounts );
399
419
})
400
420
->when ($ sortOptions , function (Builder $ query ) use ($ sortOptions ) {
401
- return $ query -> orderBy ( $ sortOptions -> orderBy , $ sortOptions-> sortOrder );
421
+ return $ this -> applyOrderBy ( $ query , $ sortOptions );
402
422
});
403
423
404
424
if ($ where ) {
0 commit comments