fix: 🐛 admin/store sorting

This commit is contained in:
IceToast 2023-02-15 21:22:16 +01:00 committed by IceToast
parent a0268f1c47
commit d905171fcb
2 changed files with 25 additions and 30 deletions

View file

@ -3,6 +3,7 @@
namespace App\Http\Controllers\Admin; namespace App\Http\Controllers\Admin;
use App\Models\ShopProduct; use App\Models\ShopProduct;
use App\Traits\DatatablesSortable;
use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View; use Illuminate\Contracts\View\View;
@ -14,6 +15,8 @@ use Illuminate\Validation\Rule;
class ShopProductController extends Controller class ShopProductController extends Controller
{ {
use DatatablesSortable;
/** /**
* Display a listing of the resource. * Display a listing of the resource.
* *
@ -66,23 +69,12 @@ class ShopProductController extends Controller
'display' => 'required|string|max:60', 'display' => 'required|string|max:60',
]); ]);
$disabled = ! is_null($request->input('disabled')); $disabled = !is_null($request->input('disabled'));
ShopProduct::create(array_merge($request->all(), ['disabled' => $disabled])); ShopProduct::create(array_merge($request->all(), ['disabled' => $disabled]));
return redirect()->route('admin.store.index')->with('success', __('Store item has been created!')); return redirect()->route('admin.store.index')->with('success', __('Store item has been created!'));
} }
/**
* Display the specified resource.
*
* @param ShopProduct $shopProduct
* @return Response
*/
public function show(ShopProduct $shopProduct)
{
//
}
/** /**
* Show the form for editing the specified resource. * Show the form for editing the specified resource.
* *
@ -116,7 +108,7 @@ class ShopProductController extends Controller
'display' => 'required|string|max:60', 'display' => 'required|string|max:60',
]); ]);
$disabled = ! is_null($request->input('disabled')); $disabled = !is_null($request->input('disabled'));
$shopProduct->update(array_merge($request->all(), ['disabled' => $disabled])); $shopProduct->update(array_merge($request->all(), ['disabled' => $disabled]));
return redirect()->route('admin.store.index')->with('success', __('Store item has been updated!')); return redirect()->route('admin.store.index')->with('success', __('Store item has been updated!'));
@ -129,7 +121,7 @@ class ShopProductController extends Controller
*/ */
public function disable(Request $request, ShopProduct $shopProduct) public function disable(Request $request, ShopProduct $shopProduct)
{ {
$shopProduct->update(['disabled' => ! $shopProduct->disabled]); $shopProduct->update(['disabled' => !$shopProduct->disabled]);
return redirect()->route('admin.store.index')->with('success', __('Product has been updated!')); return redirect()->route('admin.store.index')->with('success', __('Product has been updated!'));
} }
@ -147,19 +139,23 @@ class ShopProductController extends Controller
return redirect()->back()->with('success', __('Store item has been removed!')); return redirect()->back()->with('success', __('Store item has been removed!'));
} }
public function dataTable() public function dataTable(Request $request)
{ {
$query = ShopProduct::query(); $query = ShopProduct::query();
if ($request->has('order')) {
$query = $this->sortByColumn($request->input('order'), $request->input('columns'), $query);
}
return datatables($query) return datatables($query)
->addColumn('actions', function (ShopProduct $shopProduct) { ->addColumn('actions', function (ShopProduct $shopProduct) {
return ' return '
<a data-content="'.__('Edit').'" data-toggle="popover" data-trigger="hover" data-placement="top" href="'.route('admin.store.edit', $shopProduct->id).'" class="btn btn-sm btn-info mr-1"><i class="fas fa-pen"></i></a> <a data-content="' . __('Edit') . '" data-toggle="popover" data-trigger="hover" data-placement="top" href="' . route('admin.store.edit', $shopProduct->id) . '" class="btn btn-sm btn-info mr-1"><i class="fas fa-pen"></i></a>
<form class="d-inline" onsubmit="return submitResult();" method="post" action="'.route('admin.store.destroy', $shopProduct->id).'"> <form class="d-inline" onsubmit="return submitResult();" method="post" action="' . route('admin.store.destroy', $shopProduct->id) . '">
'.csrf_field().' ' . csrf_field() . '
'.method_field('DELETE').' ' . method_field('DELETE') . '
<button data-content="'.__('Delete').'" data-toggle="popover" data-trigger="hover" data-placement="top" class="btn btn-sm btn-danger mr-1"><i class="fas fa-trash"></i></button> <button data-content="' . __('Delete') . '" data-toggle="popover" data-trigger="hover" data-placement="top" class="btn btn-sm btn-danger mr-1"><i class="fas fa-trash"></i></button>
</form> </form>
'; ';
}) })
@ -167,12 +163,12 @@ class ShopProductController extends Controller
$checked = $shopProduct->disabled == false ? 'checked' : ''; $checked = $shopProduct->disabled == false ? 'checked' : '';
return ' return '
<form class="d-inline" onsubmit="return submitResult();" method="post" action="'.route('admin.store.disable', $shopProduct->id).'"> <form class="d-inline" onsubmit="return submitResult();" method="post" action="' . route('admin.store.disable', $shopProduct->id) . '">
'.csrf_field().' ' . csrf_field() . '
'.method_field('PATCH').' ' . method_field('PATCH') . '
<div class="custom-control custom-switch"> <div class="custom-control custom-switch">
<input '.$checked.' name="disabled" onchange="this.form.submit()" type="checkbox" class="custom-control-input" id="switch'.$shopProduct->id.'"> <input ' . $checked . ' name="disabled" onchange="this.form.submit()" type="checkbox" class="custom-control-input" id="switch' . $shopProduct->id . '">
<label class="custom-control-label" for="switch'.$shopProduct->id.'"></label> <label class="custom-control-label" for="switch' . $shopProduct->id . '"></label>
</div> </div>
</form> </form>
'; ';

View file

@ -102,10 +102,12 @@
data: 'price' data: 'price'
}, },
{ {
data: 'display' data: 'display',
sortable: false
}, },
{ {
data: 'description' data: 'description',
sortable: false
}, },
{ {
data: 'created_at' data: 'created_at'
@ -121,7 +123,4 @@
}); });
}); });
</script> </script>
@endsection @endsection