Skip to content

Commit

Permalink
Updated readme + style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Kluska committed Jan 25, 2017
1 parent bdbe186 commit 9c99d54
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
38 changes: 36 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and updates the other entries based on the models position value.

* [Installation](#installation)
* [Usage](#usage)
* [Migration example](#migration-example)
* [Events](#events)
* [Positioning](#positioning)
* [Positioned](#positioned)
Expand All @@ -30,8 +31,8 @@ composer require pion/laravel-eloquent-position
1. Add a `position` (can be custom) column in your table (model)
2. Add `PositionTrait` into your model (if you are using custom column set the `$positionColumn` property)
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)
4. Add to form the position input (can be input[type=number] and etc) and fill/set the position
5. When position is null or empty string, the last position will be used
4. Add to form the position input (can be input[type=number] and etc) and fill/set the position on save
5. When position is null or empty string, the last position will be used.

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

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

### Migration example

```php
public function up()
{
Schema::table('pages', function (Blueprint $table) {
$table->smallInteger('position')->default(0)->after('id');
});

// Update the order pages
Artisan::call('model:position', [
'model'=> \App\Models\Page\Page::class
]);
}
```

### Model example

```php
class Page extends Model
{
use PositionTrait;

public $table = 'pages';
public $positionGroup = ['parent_slug'];

protected $fillable = [
'title', 'slug', 'parent_slug', 'content', 'description', 'position'
];

}
```

### Events
You can listen to events for positioning changes. You can use the `PositionEventsTrait` for easy model registration.

Expand Down
1 change: 0 additions & 1 deletion src/Commands/RecalculatePositionCommand.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

namespace Pion\Support\Eloquent\Position\Commands;

use Pion\Support\Eloquent\Position\Traits\PositionTrait;
Expand Down
2 changes: 1 addition & 1 deletion src/PositionObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function saving($model)
$oldPosition = $model->getOriginal($model->getPositionColumn());

// Check if the position is set
if (is_null($position)) {
if (is_null($position) || $position == '') {
$this->appendLast($model, $oldPosition);
} else {
$this->move($model, $position, $oldPosition);
Expand Down

0 comments on commit 9c99d54

Please sign in to comment.