Skip to content

Commit 14842fa

Browse files
committed
Fix Sortable trait & test
Credit to @inxilpro, see laravel/framework#37956 (comment)
1 parent 928228a commit 14842fa

File tree

2 files changed

+4
-29
lines changed

2 files changed

+4
-29
lines changed

src/Database/SortableScope.php

+4-24
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
class SortableScope implements ScopeInterface
88
{
9-
protected $scopeApplied;
10-
119
/**
1210
* Apply the scope to a given Eloquent query builder.
1311
*
@@ -17,27 +15,9 @@ class SortableScope implements ScopeInterface
1715
*/
1816
public function apply(BuilderBase $builder, ModelBase $model)
1917
{
20-
$this->scopeApplied = true;
21-
22-
$builder->getQuery()->orderBy($model->getSortOrderColumn());
23-
}
24-
25-
/**
26-
* Extend the Eloquent query builder.
27-
*
28-
* @param \Illuminate\Database\Eloquent\Builder $builder
29-
* @return void
30-
*/
31-
public function extend(BuilderBase $builder)
32-
{
33-
$builder->macro('orderBy', function ($builder, $column, $direction = 'asc') {
34-
$builder
35-
->withoutGlobalScope($this)
36-
->getQuery()
37-
->orderBy($column, $direction)
38-
;
39-
40-
return $builder;
41-
});
18+
// Only apply the scope when no other explicit orders have been set
19+
if (empty($builder->getQuery()->orders) && empty($builder->getQuery()->unionOrders)) {
20+
$builder->orderBy($model->getSortOrderColumn());
21+
}
4222
}
4323
}

tests/Database/SortableTest.php

-5
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ public function testOrderByIsAutomaticallyAdded()
1212

1313
public function testOrderByCanBeOverridden()
1414
{
15-
// @TODO: Fix, see https://github.com/laravel/framework/pull/37956#issuecomment-993822397
16-
// May have to override orderBy explicitly in the Winter Builder and then check for defined
17-
// macros there before passing to parent to handle
18-
$this->markTestSkipped('@TODO: Failing');
19-
2015
$model = new TestSortableModel();
2116
$query1 = $model->newQuery()->orderBy('name')->orderBy('email', 'desc')->toSql();
2217
$query2 = $model->newQuery()->orderBy('sort_order')->orderBy('name')->toSql();

0 commit comments

Comments
 (0)