You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+42-11
Original file line number
Diff line number
Diff line change
@@ -58,12 +58,23 @@ Models simply have an integer `position` attribute corresponding to the model's
58
58
59
59
The `position` attribute is a kind of array index and is automatically inserted when a new model is created.
60
60
61
+
```php
62
+
$category = Category::create();
63
+
echo $category->position; // 0
64
+
65
+
$category = Category::create();
66
+
echo $category->position; // 1
67
+
68
+
$category = Category::create();
69
+
echo $category->position; // 2
70
+
```
71
+
61
72
The starting position gets a `0` value by default. To change that, override the `startPosition` method in the model:
62
73
63
74
```php
64
75
public function startPosition(): int
65
76
{
66
-
return 0;
77
+
return 1;
67
78
}
68
79
```
69
80
@@ -76,21 +87,21 @@ public function nextPosition(): ?int
76
87
}
77
88
```
78
89
79
-
In that example, a new model will be created in the beginning of the sequence.
90
+
In that example, a new model will be created in the beginning of the sequence and the position of other models in the sequence will be automatically incremented.
80
91
81
92
### Deleting models
82
93
83
94
When a model is deleted, the positions of other records in the sequence are updated automatically.
84
95
85
96
### Querying models
86
97
87
-
To select models in the arranged sequence, use the `orderByPosition` scope.
98
+
To select models in the arranged sequence, use the `orderByPosition` scope:
88
99
89
100
```php
90
101
Category::orderByPosition()->get();
91
102
```
92
103
93
-
### Auto ordering
104
+
### Automatic ordering
94
105
95
106
The `orderByPosition` scope is not applied by default because the corresponding SQL statement will be added to all queries, even where it is not required.
96
107
@@ -127,6 +138,12 @@ You can also use the `move` method that sets a new position value and updates th
127
138
$category->move(3);
128
139
```
129
140
141
+
If you want to move the model to the end of the sequence, you can use a negative position value:
142
+
143
+
```php
144
+
$category->move(-1); // Move to the end
145
+
```
146
+
130
147
#### Swap
131
148
132
149
The `swap` method swaps the position of two models.
By default, the package automatically updates the position of other models when the model position is updated.
141
158
142
-
If you want to update the model position without shifting the positions of other models, you can use the `withoutShifting` method:
159
+
If you want to update the model position without shifting the position of other models, you can use the `withoutShifting` method:
143
160
144
161
```php
145
162
Category::withoutShifting(function () {
146
163
$category->move(5);
147
164
})
148
165
```
149
166
150
-
#### Arrange
167
+
#### Arrange models
151
168
152
169
It is also possible to arrange models by their IDs.
153
170
154
171
The position of each model will be recalculated according to the index of its ID in the given array.
155
172
156
-
You can also provide a second argument as a starting position of the records.
173
+
You can also provide a second argument as a starting position.
157
174
158
175
```php
159
176
Category::arrangeByKeys([3, 5, 7]);
160
177
```
161
178
162
179
### Grouping / Dealing with relations
163
180
164
-
To allow models to be positioned within groups, you need to override the `newPositionQuery` method that should return a query to the grouped model sequence:
181
+
To allow models to be positioned within groups, you need to override the `newPositionQuery` method that should return a query to the grouped model sequence.
182
+
183
+
Using the relation builder:
165
184
166
185
```php
167
186
use Illuminate\Database\Eloquent\Builder;
187
+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
0 commit comments