validate([ "name" => "required|max:30", "price" => "required|numeric|max:1000000|min:0", "memory" => "required|numeric|max:1000000|min:5", "cpu" => "required|numeric|max:1000000|min:5", "swap" => "required|numeric|max:1000000|min:0", "description" => "required", "disk" => "required|numeric|max:1000000|min:5", "io" => "required|numeric|max:1000000|min:0", "databases" => "required|numeric|max:1000000|min:0", "backups" => "required|numeric|max:1000000|min:0", "allocations" => "required|numeric|max:1000000|min:0", "disabled" => "nullable", ]); $disabled = !is_null($request->input('disabled')); Product::create(array_merge($request->all(), ['disabled' => $disabled])); return redirect()->route('admin.products.index')->with('success', 'product has been created!'); } /** * Display the specified resource. * * @param Product $product * @return Application|Factory|View */ public function show(Product $product) { return view('admin.products.show', [ 'product' => $product ]); } /** * Show the form for editing the specified resource. * * @param Product $product * @return Application|Factory|View */ public function edit(Product $product) { return view('admin.products.edit', [ 'product' => $product ]); } /** * Update the specified resource in storage. * * @param Request $request * @param Product $product * @return RedirectResponse */ public function update(Request $request, Product $product): RedirectResponse { $request->validate([ "name" => "required|max:30", "price" => "required|numeric|max:1000000|min:0", "memory" => "required|numeric|max:1000000|min:5", "cpu" => "required|numeric|max:1000000|min:5", "swap" => "required|numeric|max:1000000|min:0", "description" => "required", "disk" => "required|numeric|max:1000000|min:5", "io" => "required|numeric|max:1000000|min:0", "databases" => "required|numeric|max:1000000|min:0", "backups" => "required|numeric|max:1000000|min:0", "allocations" => "required|numeric|max:1000000|min:0", "disabled" => "nullable", ]); $disabled = !is_null($request->input('disabled')); $product->update(array_merge($request->all(), ['disabled' => $disabled])); return redirect()->route('admin.products.index')->with('success', 'product has been updated!'); } /** * @param Request $request * @param Product $product * @return RedirectResponse */ public function disable(Request $request, Product $product) { $product->update(['disabled' => !$product->disabled]); return redirect()->route('admin.products.index')->with('success', 'product has been updated!'); } /** * Remove the specified resource from storage. * * @param Product $product * @return RedirectResponse */ public function destroy(Product $product) { $servers = $product->servers()->count(); if ($servers > 0) { return redirect()->back()->with('error', "Product cannot be removed while it's linked to {$servers} servers"); } $product->delete(); return redirect()->back()->with('success', 'product has been removed!'); } /** * @return JsonResponse|mixed * @throws Exception|Exception */ public function dataTable() { $query = Product::with(['servers']); return datatables($query) ->addColumn('actions', function (Product $product) { return '
' . csrf_field() . ' ' . method_field("DELETE") . '
'; }) ->addColumn('servers', function (Product $product) { return $product->servers()->count(); }) ->addColumn('disabled', function (Product $product) { $checked = $product->disabled == false ? "checked" : ""; return '
' . csrf_field() . ' ' . method_field("PATCH") . '
'; }) ->editColumn('created_at', function (Product $product) { return $product->created_at ? $product->created_at->diffForHumans() : ''; }) ->rawColumns(['actions', 'disabled']) ->make(); } }