Skip to content

Instantly share code, notes, and snippets.

@dontfreakout
Created October 15, 2018 15:28
Show Gist options
  • Save dontfreakout/934d0fbf6a161479f5b7d6491d3b27a4 to your computer and use it in GitHub Desktop.
Save dontfreakout/934d0fbf6a161479f5b7d6491d3b27a4 to your computer and use it in GitHub Desktop.
Trait which allow use only order column to reorder items in Laravel Backpack.
<?php
namespace App\Http\Controllers\Admin\Contracts;
/**
* Instead of "Nested Set pattern" use only "order" column to reorder the items in the database.
* Database columns needed: id, name, order
*
* @return
*/
trait SimpleReorderTrait
{
public function saveReorder()
{
$this->crud->hasAccessOrFail('reorder');
$all_entries = \Request::input('tree');
if (count($all_entries)) {
$count = 0;
foreach ($all_entries as $key => $entry) {
if ($entry['item_id'] != '' && $entry['item_id'] != null) {
$item = $this->crud->model->find($entry['item_id']);
$item->order = empty($entry['left']) ? null : $entry['left'];
$item->save();
$count++;
}
}
} else {
return false;
}
return 'success for '.$count.' items';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment