5
5
use ArrayAccess ;
6
6
use Illuminate \Database \Eloquent \Builder ;
7
7
use Illuminate \Database \Eloquent \SoftDeletingScope ;
8
+ use Illuminate \Support \Facades \Event ;
8
9
use InvalidArgumentException ;
9
10
10
11
trait SortableTrait
@@ -27,22 +28,26 @@ public function setHighestOrderNumber(): void
27
28
28
29
public function getHighestOrderNumber (): int
29
30
{
30
- return (int ) $ this ->buildSortQuery ()->max ($ this ->determineOrderColumnName ());
31
+ return (int )$ this ->buildSortQuery ()->max ($ this ->determineOrderColumnName ());
31
32
}
32
33
33
34
public function getLowestOrderNumber (): int
34
35
{
35
- return (int ) $ this ->buildSortQuery ()->min ($ this ->determineOrderColumnName ());
36
+ return (int )$ this ->buildSortQuery ()->min ($ this ->determineOrderColumnName ());
36
37
}
37
38
38
39
public function scopeOrdered (Builder $ query , string $ direction = 'asc ' )
39
40
{
40
41
return $ query ->orderBy ($ this ->determineOrderColumnName (), $ direction );
41
42
}
42
43
43
- public static function setNewOrder ($ ids , int $ startOrder = 1 , string $ primaryKeyColumn = null , callable $ modifyQuery = null ): void
44
- {
45
- if (! is_array ($ ids ) && ! $ ids instanceof ArrayAccess) {
44
+ public static function setNewOrder (
45
+ $ ids ,
46
+ int $ startOrder = 1 ,
47
+ string $ primaryKeyColumn = null ,
48
+ callable $ modifyQuery = null
49
+ ): void {
50
+ if (!is_array ($ ids ) && !$ ids instanceof ArrayAccess) {
46
51
throw new InvalidArgumentException ('You must pass an array or ArrayAccess object to setNewOrder ' );
47
52
}
48
53
@@ -67,7 +72,7 @@ public static function setNewOrder($ids, int $startOrder = 1, string $primaryKey
67
72
->update ([$ orderColumnName => $ startOrder ++]);
68
73
}
69
74
70
- event (new EloquentModelSortedEvent (static ::class));
75
+ Event:: dispatch (new EloquentModelSortedEvent (static ::class));
71
76
72
77
if (config ('eloquent-sortable.ignore_timestamps ' , false )) {
73
78
static ::$ ignoreTimestampsOn = array_values (array_diff (static ::$ ignoreTimestampsOn , [static ::class]));
@@ -101,7 +106,7 @@ public function moveOrderDown(): static
101
106
->where ($ orderColumnName , '> ' , $ this ->$ orderColumnName )
102
107
->first ();
103
108
104
- if (! $ swapWithModel ) {
109
+ if (!$ swapWithModel ) {
105
110
return $ this ;
106
111
}
107
112
@@ -117,7 +122,7 @@ public function moveOrderUp(): static
117
122
->where ($ orderColumnName , '< ' , $ this ->$ orderColumnName )
118
123
->first ();
119
124
120
- if (! $ swapWithModel ) {
125
+ if (!$ swapWithModel ) {
121
126
return $ this ;
122
127
}
123
128
@@ -159,7 +164,9 @@ public function moveToStart(): static
159
164
$ this ->$ orderColumnName = $ firstModel ->$ orderColumnName ;
160
165
$ this ->save ();
161
166
162
- $ this ->buildSortQuery ()->where ($ this ->getQualifiedKeyName (), '!= ' , $ this ->getKey ())->increment ($ orderColumnName );
167
+ $ this ->buildSortQuery ()->where ($ this ->getQualifiedKeyName (), '!= ' , $ this ->getKey ())->increment (
168
+ $ orderColumnName
169
+ );
163
170
164
171
return $ this ;
165
172
}
0 commit comments