Comments (3)
This strategy would probably not be efficient in large tables, so the package doesn’t do that.
If you want this behaviour, add it to your own project
from eloquent-sortable.
@freekmurze makes sense, thanks.
Here's my early attempt incase anyone else lands here:
static::saved(function($model) {
if($model->wasChanged('order')) {
$ids = static::ordered()->pluck('id');
static::setNewOrder($ids);
}
});
Order not perfect yet & I actually needed additional scope:
$ids = static::where('group_id', $model->group_id)->ordered()->pluck('id');
from eloquent-sortable.
As i noticed the comment for the attempt, i wanted to share my own implementation as i encountered this same problem. I created a portable support class in laravel for this. Feel free to use/abuse when needed.
Usage:
$wantedOrder = 5;
$model = Model::create();
\App\Support\Sortable::set($wantedOrder, $model);
Support Class:
<?php
namespace App\Support;
use Illuminate\Database\Eloquent\Model;
class Sortable
{
public static function set(int $sequence, Model $model, string $field = 'order', int $step = 1): void
{
if ($model->getAttribute($field) === null) {
throw new \LogicException('Model does not seem to have the configured field');
}
// Store id for usage in where not
$modelId = $model->id;
// Set sequence value for requested model
$model->{$field} = $sequence;
$model->save();
// Now increment every other model until a gap is found with only 1 step
$newSequence = $sequence + 1;
$model::query()
->where($field, '>=', $sequence)
->whereNot('id', $modelId)
->orderBy($field)
->each(function (Model $model) use (&$newSequence, $field) {
if ($model->{$field} > $newSequence) {
// If a sequence gap is found, abort looping as it's working as intended
return false;
}
$model->{$field} = $newSequence;
$model->save();
$newSequence++;
return true;
});
// Now reset all model sequences for possible step setup
self::reconfigure($model, $field, $step);
}
public static function reconfigure(Model $model, string $field = 'order', int $step = 1): void
{
$sequence = 0;
$model::query()
->where($field, '>=', 0)
->orderBy($field)
->each(function (Model $model) use (&$sequence, $step, $field) {
$sequence = $sequence + $step;
$model->{$field} = $sequence;
$model->save();
});
}
}
from eloquent-sortable.
Related Issues (20)
- Don't work EloquentSortable HOT 2
- Feature request: Zero based index
- How to sort all by desc in a resource?
- Changing the sort breaks the sort if the model uses scopeOrdered() HOT 1
- Not firing model events when changing order HOT 4
- Order is getting overwritten when creating HOT 2
- syntax error, unexpected 'static' (T_STATIC) in SortableTrait.php
- Possible Duplicate Order Values?
- actions belongsToMany
- Readme is inconsistent. HOT 1
- Nullable Order HOT 1
- Unknown column 'order_column'
- Sort when creating doesn't work with polymrphic pivot models.
- Sort values not generated on MorphPivot table
- Add ability to reorder model before/after another HOT 2
- Problems when using ordered or orderBy HOT 2
- Disabling sorting when replicating
- 有一种情况下, 调用 moveOrderUp 上移操作可能会不准确。 HOT 2
- setNewOrder ignores "buildSortQuery"
- Duplication of item (hasMany) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from eloquent-sortable.