Skip to content

Instantly share code, notes, and snippets.

@adumskis
Created July 7, 2016 07:30
Show Gist options
  • Save adumskis/eaaacfa8dd9ac1b9f9279a0e0f8e32fd to your computer and use it in GitHub Desktop.
Save adumskis/eaaacfa8dd9ac1b9f9279a0e0f8e32fd to your computer and use it in GitHub Desktop.
For Rutorika\Sortable to fix position field before deleting eloquent
<?php
/**
* For Rutorika\Sortable to fix position field before deleting eloquent
*/
namespace App\Traits;
trait SortableFixTrait
{
public static function bootSortableFixTrait()
{
static::deleting(
function ($model) {
/* @var Model $model */
$sortableField = static::getSortableField();
$query = static::applySortableGroup(static::on(), $model);
$models = $query->where('id', '<>', $model->id)->sorted()->get();
$position = 1;
foreach($models as $key => $model){
$model->update([$sortableField => $position]);
$position++;
}
}
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment