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
+28-7
Original file line number
Diff line number
Diff line change
@@ -58,17 +58,26 @@ 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
-
By default, the model takes a position at the very end of the sequence.
62
-
63
-
The initial position gets a `0` value by default. To change that, override the `getInitPosition` method in the model.
61
+
The starting position gets a `0` value by default. To change that, override the `startPosition` method in the model:
64
62
65
63
```php
66
-
public function getInitPosition(): int
64
+
public function startPosition(): int
67
65
{
68
66
return 0;
69
67
}
70
68
```
71
69
70
+
By default, the created model takes a position at the very end of the sequence. If you need to customize that behaviour, you can override the `nextPosition` method:
71
+
72
+
```php
73
+
public function nextPosition(): ?int
74
+
{
75
+
return $this->startPosition();
76
+
}
77
+
```
78
+
79
+
In that example, a new model will be created in the beginning of the sequence.
80
+
72
81
### Deleting models
73
82
74
83
When a model is deleted, the positions of other records in the sequence are updated automatically.
@@ -110,12 +119,12 @@ $category->update([
110
119
111
120
The positions of other models will be automatically recalculated as well.
112
121
113
-
#### Move
122
+
#### Shift / Move
114
123
115
-
You can also use the `move` method that sets a new position value and updates the model immediately:
124
+
You can also use the `shift method that sets a new position value and updates the model immediately:
116
125
117
126
```php
118
-
$category->move(3);
127
+
$category->shift(3);
119
128
```
120
129
121
130
#### Swap
@@ -126,6 +135,18 @@ The `swap` method swaps the position of two models.
126
135
$category->swap($anotherCategory);
127
136
```
128
137
138
+
#### Without shifting
139
+
140
+
By default, the package automatically updates the position of other models when the model position is updated.
141
+
142
+
If you want to update the model position without shifting the positions of other models, you can use the `withoutShifting` method:
143
+
144
+
```php
145
+
Category::withoutShifting(function () {
146
+
$category->move(5);
147
+
})
148
+
```
149
+
129
150
#### Arrange
130
151
131
152
It is also possible to arrange models by their IDs.
0 commit comments