Skip to content

Commit

Permalink
Merge pull request #5 from digitalsign/master
Browse files Browse the repository at this point in the history
修复 #1 路由缓存报错的问题
  • Loading branch information
z-song authored Feb 18, 2020
2 parents 6fc0af7 + f6da0ed commit d7245c2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 34 deletions.
35 changes: 1 addition & 34 deletions routes/web.php
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');
43 changes: 43 additions & 0 deletions src/Controllers/GridSortableController.php
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'));
}
}

0 comments on commit d7245c2

Please sign in to comment.