Skip to content

Instantly share code, notes, and snippets.

@adumskis
Created February 22, 2017 13:03
Show Gist options
  • Save adumskis/ad3a3a94af195a0e6dda7890bfa6529a to your computer and use it in GitHub Desktop.
Save adumskis/ad3a3a94af195a0e6dda7890bfa6529a to your computer and use it in GitHub Desktop.
Event listener to prevent empty spaces in sortable list
<?php
namespace App\Providers;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Event::listen('eloquent.deleted:*', function ($model) {
$reflector = new \ReflectionClass($model);
$traits = $reflector->getTraitNames();
if(in_array('Rutorika\Sortable\SortableTrait', $traits)){
$namespace = $reflector->getName();
$position_field = $namespace::getSortableField();
$model->next()->decrement($position_field);
}
return true;
});
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment