Created
July 7, 2016 07:30
-
-
Save adumskis/eaaacfa8dd9ac1b9f9279a0e0f8e32fd to your computer and use it in GitHub Desktop.
For Rutorika\Sortable to fix position field before deleting eloquent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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