From 60eab08735187044240c3f180867411c9b772813 Mon Sep 17 00:00:00 2001 From: snipe Date: Sat, 21 Jan 2023 14:41:07 -0800 Subject: [PATCH] =?UTF-8?q?Added=20`->with(=E2=80=98users=20as=20users=5Fc?= =?UTF-8?q?ount=E2=80=99)`=20in=20Accessory=20query?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: snipe --- .../Accessories/AccessoriesController.php | 74 +++++++++---------- .../Controllers/Api/AccessoriesController.php | 5 +- 2 files changed, 41 insertions(+), 38 deletions(-) diff --git a/app/Http/Controllers/Accessories/AccessoriesController.php b/app/Http/Controllers/Accessories/AccessoriesController.php index d1af79adf1..96f83d7665 100755 --- a/app/Http/Controllers/Accessories/AccessoriesController.php +++ b/app/Http/Controllers/Accessories/AccessoriesController.php @@ -127,45 +127,45 @@ class AccessoriesController extends Controller */ public function update(ImageUploadRequest $request, $accessoryId = null) { - if (is_null($accessory = Accessory::find($accessoryId))) { + if ($accessory = Accessory::withCount('users as users_count')->find($accessoryId)) { + + $validator = Validator::make($request->all(), [ + "qty" => "required|numeric|min:$accessory->users_count" + ]); + + if ($validator->fails()) { + return redirect()->back() + ->withErrors($validator) + ->withInput(); + } + + $this->authorize($accessory); + + // Update the accessory data + $accessory->name = request('name'); + $accessory->location_id = request('location_id'); + $accessory->min_amt = request('min_amt'); + $accessory->category_id = request('category_id'); + $accessory->company_id = Company::getIdForCurrentUser(request('company_id')); + $accessory->manufacturer_id = request('manufacturer_id'); + $accessory->order_number = request('order_number'); + $accessory->model_number = request('model_number'); + $accessory->purchase_date = request('purchase_date'); + $accessory->purchase_cost = Helper::ParseCurrency(request('purchase_cost')); + $accessory->qty = request('qty'); + $accessory->supplier_id = request('supplier_id'); + $accessory->notes = request('notes'); + + $accessory = $request->handleImages($accessory); + + // Was the accessory updated? + if ($accessory->save()) { + return redirect()->route('accessories.index')->with('success', trans('admin/accessories/message.update.success')); + } + } else { return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.does_not_exist')); } - $min = $accessory->numCheckedOut(); - $validator = Validator::make($request->all(), [ - "qty" => "required|numeric|min:$min" - ]); - - if ($validator->fails()) { - return redirect()->back() - ->withErrors($validator) - ->withInput(); - } - - $this->authorize($accessory); - - // Update the accessory data - $accessory->name = request('name'); - $accessory->location_id = request('location_id'); - $accessory->min_amt = request('min_amt'); - $accessory->category_id = request('category_id'); - $accessory->company_id = Company::getIdForCurrentUser(request('company_id')); - $accessory->manufacturer_id = request('manufacturer_id'); - $accessory->order_number = request('order_number'); - $accessory->model_number = request('model_number'); - $accessory->purchase_date = request('purchase_date'); - $accessory->purchase_cost = Helper::ParseCurrency(request('purchase_cost')); - $accessory->qty = request('qty'); - $accessory->supplier_id = request('supplier_id'); - $accessory->notes = request('notes'); - - $accessory = $request->handleImages($accessory); - - // Was the accessory updated? - if ($accessory->save()) { - return redirect()->route('accessories.index')->with('success', trans('admin/accessories/message.update.success')); - } - return redirect()->back()->withInput()->withErrors($accessory->getErrors()); } @@ -217,7 +217,7 @@ class AccessoriesController extends Controller */ public function show($accessoryID = null) { - $accessory = Accessory::find($accessoryID); + $accessory = Accessory::withCount('users as users_count')->find($accessoryID); $this->authorize('view', $accessory); if (isset($accessory->id)) { return view('accessories/view', compact('accessory')); diff --git a/app/Http/Controllers/Api/AccessoriesController.php b/app/Http/Controllers/Api/AccessoriesController.php index a894dc3760..fd21ebaf3a 100644 --- a/app/Http/Controllers/Api/AccessoriesController.php +++ b/app/Http/Controllers/Api/AccessoriesController.php @@ -41,10 +41,13 @@ class AccessoriesController extends Controller 'min_amt', 'company_id', 'notes', + 'users_count', + 'qty', ]; - $accessories = Accessory::select('accessories.*')->with('category', 'company', 'manufacturer', 'users', 'location', 'supplier'); + $accessories = Accessory::select('accessories.*')->with('category', 'company', 'manufacturer', 'users', 'location', 'supplier') + ->withCount('users as users_count'); if ($request->filled('search')) { $accessories = $accessories->TextSearch($request->input('search'));