File tree 2 files changed +44
-34
lines changed
2 files changed +44
-34
lines changed Original file line number Diff line number Diff line change 1
1
<?php
2
2
3
- Route::post ('_grid-sortable_ ' , function (\Illuminate \Http \Request $ request ) {
4
-
5
- $ sorts = $ request ->get ('_sort ' );
6
-
7
- $ sorts = collect ($ sorts )
8
- ->pluck ('key ' )
9
- ->combine (
10
- collect ($ sorts )->pluck ('sort ' )->sort ()
11
- );
12
-
13
- $ status = true ;
14
- $ message = trans ('admin.save_succeeded ' );
15
- $ modelClass = $ request ->get ('_model ' );
16
-
17
- try {
18
- /** @var \Illuminate\Database\Eloquent\Collection $models */
19
- $ models = $ modelClass ::find ($ sorts ->keys ());
20
-
21
- foreach ($ models as $ model ) {
22
-
23
- $ column = data_get ($ model ->sortable , 'order_column_name ' , 'order_column ' );
24
-
25
- $ model ->{$ column } = $ sorts ->get ($ model ->getKey ());
26
- $ model ->save ();
27
- }
28
-
29
- } catch (Exception $ exception ) {
30
- $ status = false ;
31
- $ message = $ exception ->getMessage ();
32
- }
33
-
34
- return response ()->json (compact ('status ' , 'message ' ));
35
-
36
- })->name ('laravel-admin-grid-sortable ' );
3
+ Route::post ('_grid-sortable_ ' , '\Encore\Admin\GridSortable\Controllers\GridSortableController@sort ' )->name ('laravel-admin-grid-sortable ' );
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Encore \Admin \GridSortable \Controllers ;
4
+
5
+ use Exception ;
6
+ use Illuminate \Http \Request ;
7
+ use Illuminate \Routing \Controller ;
8
+
9
+ class GridSortableController extends Controller
10
+ {
11
+ public function sort (Request $ request )
12
+ {
13
+ $ sorts = $ request ->get ('_sort ' );
14
+
15
+ $ sorts = collect ($ sorts )
16
+ ->pluck ('key ' )
17
+ ->combine (
18
+ collect ($ sorts )->pluck ('sort ' )->sort ()
19
+ );
20
+
21
+ $ status = true ;
22
+ $ message = trans ('admin.save_succeeded ' );
23
+ $ modelClass = $ request ->get ('_model ' );
24
+
25
+ try {
26
+ /** @var \Illuminate\Database\Eloquent\Collection $models */
27
+ $ models = $ modelClass ::find ($ sorts ->keys ());
28
+
29
+ foreach ($ models as $ model ) {
30
+
31
+ $ column = data_get ($ model ->sortable , 'order_column_name ' , 'order_column ' );
32
+
33
+ $ model ->{$ column } = $ sorts ->get ($ model ->getKey ());
34
+ $ model ->save ();
35
+ }
36
+ } catch (Exception $ exception ) {
37
+ $ status = false ;
38
+ $ message = $ exception ->getMessage ();
39
+ }
40
+
41
+ return response ()->json (compact ('status ' , 'message ' ));
42
+ }
43
+ }
You can’t perform that action at this time.
0 commit comments