Created
February 22, 2017 13:03
-
-
Save adumskis/ad3a3a94af195a0e6dda7890bfa6529a to your computer and use it in GitHub Desktop.
Event listener to prevent empty spaces in sortable list
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 | |
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