Skip to content

Commit 9c99d54

Browse files
author
Martin Kluska
committed
Updated readme + style fixes
1 parent bdbe186 commit 9c99d54

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

README.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and updates the other entries based on the models position value.
88

99
* [Installation](#installation)
1010
* [Usage](#usage)
11+
* [Migration example](#migration-example)
1112
* [Events](#events)
1213
* [Positioning](#positioning)
1314
* [Positioned](#positioned)
@@ -30,8 +31,8 @@ composer require pion/laravel-eloquent-position
3031
1. Add a `position` (can be custom) column in your table (model)
3132
2. Add `PositionTrait` into your model (if you are using custom column set the `$positionColumn` property)
3233
3. If you are using grouped entries (like parent_id and etc), you can set the `$positionGroup` with the column name/names (supports single string or multiple columns)
33-
4. Add to form the position input (can be input[type=number] and etc) and fill/set the position
34-
5. When position is null or empty string, the last position will be used
34+
4. Add to form the position input (can be input[type=number] and etc) and fill/set the position on save
35+
5. When position is null or empty string, the last position will be used.
3536

3637
**Then you can get your entries sorted:**
3738

@@ -48,6 +49,39 @@ If using default column name (position), the value will be converted to numeric
4849
**Get the position**
4950
Use the `$model->getPosition()` or use the standard way by using the column name `$model->position`
5051

52+
### Migration example
53+
54+
```php
55+
public function up()
56+
{
57+
Schema::table('pages', function (Blueprint $table) {
58+
$table->smallInteger('position')->default(0)->after('id');
59+
});
60+
61+
// Update the order pages
62+
Artisan::call('model:position', [
63+
'model'=> \App\Models\Page\Page::class
64+
]);
65+
}
66+
```
67+
68+
### Model example
69+
70+
```php
71+
class Page extends Model
72+
{
73+
use PositionTrait;
74+
75+
public $table = 'pages';
76+
public $positionGroup = ['parent_slug'];
77+
78+
protected $fillable = [
79+
'title', 'slug', 'parent_slug', 'content', 'description', 'position'
80+
];
81+
82+
}
83+
```
84+
5185
### Events
5286
You can listen to events for positioning changes. You can use the `PositionEventsTrait` for easy model registration.
5387

src/Commands/RecalculatePositionCommand.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
namespace Pion\Support\Eloquent\Position\Commands;
43

54
use Pion\Support\Eloquent\Position\Traits\PositionTrait;

src/PositionObserver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function saving($model)
4848
$oldPosition = $model->getOriginal($model->getPositionColumn());
4949

5050
// Check if the position is set
51-
if (is_null($position)) {
51+
if (is_null($position) || $position == '') {
5252
$this->appendLast($model, $oldPosition);
5353
} else {
5454
$this->move($model, $position, $oldPosition);

0 commit comments

Comments
 (0)