-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6fc0af7
commit f6da0ed
Showing
2 changed files
with
44 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,3 @@ | ||
<?php | ||
|
||
Route::post('_grid-sortable_', function (\Illuminate\Http\Request $request) { | ||
|
||
$sorts = $request->get('_sort'); | ||
|
||
$sorts = collect($sorts) | ||
->pluck('key') | ||
->combine( | ||
collect($sorts)->pluck('sort')->sort() | ||
); | ||
|
||
$status = true; | ||
$message = trans('admin.save_succeeded'); | ||
$modelClass = $request->get('_model'); | ||
|
||
try { | ||
/** @var \Illuminate\Database\Eloquent\Collection $models */ | ||
$models = $modelClass::find($sorts->keys()); | ||
|
||
foreach ($models as $model) { | ||
|
||
$column = data_get($model->sortable, 'order_column_name', 'order_column'); | ||
|
||
$model->{$column} = $sorts->get($model->getKey()); | ||
$model->save(); | ||
} | ||
|
||
} catch (Exception $exception) { | ||
$status = false; | ||
$message = $exception->getMessage(); | ||
} | ||
|
||
return response()->json(compact('status', 'message')); | ||
|
||
})->name('laravel-admin-grid-sortable'); | ||
Route::post('_grid-sortable_', '\Encore\Admin\GridSortable\Controllers\GridSortableController@sort')->name('laravel-admin-grid-sortable'); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Encore\Admin\GridSortable\Controllers; | ||
|
||
use Exception; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Routing\Controller; | ||
|
||
class GridSortableController extends Controller | ||
{ | ||
public function sort(Request $request) | ||
{ | ||
$sorts = $request->get('_sort'); | ||
|
||
$sorts = collect($sorts) | ||
->pluck('key') | ||
->combine( | ||
collect($sorts)->pluck('sort')->sort() | ||
); | ||
|
||
$status = true; | ||
$message = trans('admin.save_succeeded'); | ||
$modelClass = $request->get('_model'); | ||
|
||
try { | ||
/** @var \Illuminate\Database\Eloquent\Collection $models */ | ||
$models = $modelClass::find($sorts->keys()); | ||
|
||
foreach ($models as $model) { | ||
|
||
$column = data_get($model->sortable, 'order_column_name', 'order_column'); | ||
|
||
$model->{$column} = $sorts->get($model->getKey()); | ||
$model->save(); | ||
} | ||
} catch (Exception $exception) { | ||
$status = false; | ||
$message = $exception->getMessage(); | ||
} | ||
|
||
return response()->json(compact('status', 'message')); | ||
} | ||
} |