Coder Social home page Coder Social logo

Comments (3)

freekmurze avatar freekmurze commented on May 16, 2024

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.

dillingham avatar dillingham commented on May 16, 2024

@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.

bmoex avatar bmoex commented on May 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.