Skip to content

Commit 70db357

Browse files
committed
Change ordering
1 parent 961c59e commit 70db357

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ or with conditions
9898
```php
9999
$invoices = Invoice::orderByRelation(['items:price' => function (Builder $query) {
100100
$query->where('price', '>', 6);
101-
}, 'max', 'desc'])->get();
101+
}, 'desc', 'max'])->get();
102102
```
103103
By default, sorting is by `max` and `desc`, you can choose one of the options `max`, `min`, `sum`, `avg`, `desc`, `acs`.
104104
```php
105-
$invoices = Invoice::orderByRelation('items:price', 'sum', 'asc')->get();
105+
$invoices = Invoice::orderByRelation('items:price', 'asc', 'sum')->get();
106106
```

src/LaravelSubQuery.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,16 @@ public function withAvg($relations)
5959
return $this->withSubQuery($relations, 'avg');
6060
}
6161

62-
public function orderByRelation($relations, $type = 'max', $orderType = 'desc')
62+
public function orderByRelation($relations, $orderType = 'desc', $type = 'max')
6363
{
6464
if (is_array($relations)) {
65-
$type = $relations[0];
66-
$orderType = $relations[1];
65+
$orderType = $relations[0];
66+
$type = $relations[1];
6767
unset($relations[0], $relations[1]);
6868
}
69-
69+
if (!strpos($relations, ':')) {
70+
return $this->orderBy($relations, $orderType);
71+
}
7072
return $this->withSubQuery($relations, $type, $orderType);
7173
}
7274

0 commit comments

Comments
 (0)