Skip to content

Commit

Permalink
Merge branch 'master' of github.com:spatie/eloquent-sortable
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Feb 27, 2019
2 parents c60109a + e16a81e commit f62a881
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
2 changes: 0 additions & 2 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
preset: laravel

linting: true
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
language: php

php:
- 7.0
- 7.1
- 7.2
- 7.3

env:
matrix:
Expand Down
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


[![Latest Version](https://img.shields.io/github/release/spatie/eloquent-sortable.svg?style=flat-square)](https://github.com/spatie/eloquent-sortable/releases)
[![SensioLabsInsight](https://img.shields.io/sensiolabs/i/fb7765b9-7632-4897-8054-22d85b41ffda.svg)](https://insight.sensiolabs.com/projects/fb7765b9-7632-4897-8054-22d85b41ffda)
[![Build Status](https://img.shields.io/travis/spatie/eloquent-sortable.svg?style=flat-square)](https://travis-ci.org/spatie/eloquent-sortable)
[![Quality Score](https://img.shields.io/scrutinizer/g/spatie/eloquent-sortable.svg?style=flat-square)](https://scrutinizer-ci.com/g/spatie/eloquent-sortable)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
Expand All @@ -22,15 +21,15 @@ Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all
This package can be installed through Composer.

```
$ composer require spatie/eloquent-sortable
composer require spatie/eloquent-sortable
```

## Usage

To add sortable behaviour to your model you must:<br />
1. specify that the model will conform to ```Spatie\EloquentSortable\Sortable```<br />
2. use the trait ```Spatie\EloquentSortable\SortableTrait```<br />
3. specify which column will be used as the order column<br />
To add sortable behaviour to your model you must:
1. Implement the `Spatie\EloquentSortable\Sortable` interface.
2. Use the trait `Spatie\EloquentSortable\SortableTrait`.
3. Optionally specify which column will be used as the order column. The default is `order_column`.

### Example

Expand Down Expand Up @@ -115,6 +114,19 @@ You can swap the order of two models:
MyModel::swapOrder($myModel, $anotherModel);
```

### Grouping

If your model/table has a grouping field (usually a foreign key): `id, `**`user_id`**`, title, order_column`
and you'd like the above methods to take it into considerations, you can create a `buildSortQuery` method at your model:
```php
public function buildSortQuery()
{
return static::query()->where('user_id', $this->user_id);
}
```
This will restrict the calculations to fields value of the model instance.


## Tests

The package contains some integration/smoke tests, set up with Orchestra. The tests can be run via phpunit.
Expand Down

0 comments on commit f62a881

Please sign in to comment.