Created
October 15, 2018 15:28
-
-
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.
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\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