From f6da0ed6fda25598612a30c2465a84c3be8e776e Mon Sep 17 00:00:00 2001 From: "xiaohui.lam" Date: Mon, 17 Feb 2020 02:20:55 +0800 Subject: [PATCH] resolved #1 --- routes/web.php | 35 +----------------- src/Controllers/GridSortableController.php | 43 ++++++++++++++++++++++ 2 files changed, 44 insertions(+), 34 deletions(-) create mode 100644 src/Controllers/GridSortableController.php diff --git a/routes/web.php b/routes/web.php index 0b7def7..b1fa7c7 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,36 +1,3 @@ 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'); \ No newline at end of file +Route::post('_grid-sortable_', '\Encore\Admin\GridSortable\Controllers\GridSortableController@sort')->name('laravel-admin-grid-sortable'); diff --git a/src/Controllers/GridSortableController.php b/src/Controllers/GridSortableController.php new file mode 100644 index 0000000..e36c4db --- /dev/null +++ b/src/Controllers/GridSortableController.php @@ -0,0 +1,43 @@ +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')); + } +}