Skip to content

Commit 6c038d9

Browse files
author
Martin Kluska
committed
first commit
0 parents  commit 6c038d9

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

README.md

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# Laravel eloquent model position
2+
Position logic for Eloquent models with minimum setup. Before saving it will check if the position has changed
3+
and updates the other entries based on the models position value.
4+
5+
[![Total Downloads](https://poser.pugx.org/pion/laravel-eloquent-position/downloads?format=flat)](https://packagist.org/packages/pion/laravel-eloquent-position)
6+
[![Latest Stable Version](https://poser.pugx.org/pion/laravel-eloquent-position/v/stable?format=flat)](https://packagist.org/packages/pion/laravel-eloquent-position)
7+
[![Latest Unstable Version](https://poser.pugx.org/pion/laravel-eloquent-position/v/unstable?format=flat)](https://packagist.org/packages/pion/laravel-eloquent-position)
8+
9+
* [Installation](#installation)
10+
* [Usage](#usage)
11+
* [Events](#events)
12+
* [Positioning](#positioning)
13+
* [Positioned](#positioned)
14+
* [Command](#command)
15+
* [Traits](#traits)
16+
* [Changelog](#changelog)
17+
* [Todo](#todo)
18+
* [Contribution](#contribution)
19+
20+
## Installation
21+
22+
**Install via composer**
23+
24+
```
25+
composer require pion/laravel-eloquent-position
26+
```
27+
28+
## Usage
29+
30+
1. Add a `position` (can be custom) column in your table (model)
31+
2. Add `PositionTrait` into your model (if you are using custom column set the `$positionColumn` property)
32+
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
35+
36+
**Then you can get your entries sorted:**
37+
38+
```php
39+
// ASC
40+
YourModel::sorted()->get()
41+
42+
// DESC
43+
YourModel::sortedByDESC()->get()
44+
```
45+
46+
If using default column name (position), the value will be converted to numeric value (if not null or empty string).
47+
48+
**Get the position**
49+
Use the `$model->getPosition()` or use the standard way by using the column name `$model->position`
50+
51+
### Events
52+
You can listen to events for positioning changes. You can use the `PositionEventsTrait` for easy model registration.
53+
54+
```php
55+
....
56+
57+
class YourModel extends Model {
58+
use PositionTrait, PositionEventsTrait;
59+
....
60+
}
61+
```
62+
63+
#### Positioning
64+
Called before running the last position calculation and the final movement of other entries for given position.
65+
66+
**Enables to:**
67+
* Restore the position to original value - return false
68+
* Add additional query conditions via AbstractPositionQuery object in second parameter ($query->query() => Builder)
69+
70+
Name: `positioning`
71+
72+
```php
73+
YourModel::positioning(function($model, $query) {
74+
$query->query()->where('type', 'type'); // or etc
75+
\Log::info('positioning', 'To '.$model->getPosition().' from '.$query->oldPosition());
76+
});
77+
```
78+
79+
#### Positioned
80+
81+
Name: `positioned`
82+
83+
Example via trait:
84+
85+
```php
86+
YourModel::positioned(function($model) {
87+
/// TODO
88+
});
89+
```
90+
91+
### Command
92+
93+
#### Reposition command
94+
This command will help you to fix the order of your models. You must provide a model class.
95+
You must include the `RecalculatePositionCommand` into your Console `Kernel` class.
96+
97+
```bash
98+
php artisan model:position App\\Models\\YourModel
99+
```
100+
101+
### Traits
102+
103+
#### PositionTrait
104+
Uses the `BasePositionTrait` and `PositionScopeTrait`
105+
106+
You can set:
107+
* *string* `positionColumn` *to enable overriding for the position column*
108+
* *boolean* `disablePositionUpdate` *disables the updated of other entries*
109+
* *string|array* `positionGroup` *builds a filter from columns for position calculation. Supports single column or multiple columns*
110+
111+
#### PositionScopeTrait
112+
113+
114+
## Todo
115+
116+
- [ ] Add the custom position trait to enable automatic convert to numeric value (don't want to use the setAttribute method) - In progress
117+
- [ ] Add service provider for automatic command registration
118+
- [ ] Add all docs for all features
119+
- [ ] Add next/prev scope functions in `PositionScopeTrait`
120+
- [ ] Add `PositionHelperTrait` with (getLastUsedPosition, getNextPosition($position = null))
121+
122+
## Contribution
123+
See [CONTRIBUTING.md](CONTRIBUTING.md) for how to contribute changes. All contributions are welcome.
124+
125+
## Copyright and License
126+
127+
[laravel-eloquent-position](https://github.com/pionl/laravel-eloquent-position)
128+
was written by [Martin Kluska](http://kluska.cz) and is released under the
129+
[MIT License](LICENSE.md).
130+
131+
Copyright (c) 2016 Martin Kluska

0 commit comments

Comments
 (0)