Added ->with(‘users as users_count’) in Accessory query

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2023-01-21 14:41:07 -08:00
parent 90c1631820
commit 60eab08735
2 changed files with 41 additions and 38 deletions

View file

@ -127,13 +127,10 @@ class AccessoriesController extends Controller
*/ */
public function update(ImageUploadRequest $request, $accessoryId = null) public function update(ImageUploadRequest $request, $accessoryId = null)
{ {
if (is_null($accessory = Accessory::find($accessoryId))) { if ($accessory = Accessory::withCount('users as users_count')->find($accessoryId)) {
return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.does_not_exist'));
}
$min = $accessory->numCheckedOut();
$validator = Validator::make($request->all(), [ $validator = Validator::make($request->all(), [
"qty" => "required|numeric|min:$min" "qty" => "required|numeric|min:$accessory->users_count"
]); ]);
if ($validator->fails()) { if ($validator->fails()) {
@ -165,6 +162,9 @@ class AccessoriesController extends Controller
if ($accessory->save()) { if ($accessory->save()) {
return redirect()->route('accessories.index')->with('success', trans('admin/accessories/message.update.success')); 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'));
}
return redirect()->back()->withInput()->withErrors($accessory->getErrors()); return redirect()->back()->withInput()->withErrors($accessory->getErrors());
} }
@ -217,7 +217,7 @@ class AccessoriesController extends Controller
*/ */
public function show($accessoryID = null) public function show($accessoryID = null)
{ {
$accessory = Accessory::find($accessoryID); $accessory = Accessory::withCount('users as users_count')->find($accessoryID);
$this->authorize('view', $accessory); $this->authorize('view', $accessory);
if (isset($accessory->id)) { if (isset($accessory->id)) {
return view('accessories/view', compact('accessory')); return view('accessories/view', compact('accessory'));

View file

@ -41,10 +41,13 @@ class AccessoriesController extends Controller
'min_amt', 'min_amt',
'company_id', 'company_id',
'notes', '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')) { if ($request->filled('search')) {
$accessories = $accessories->TextSearch($request->input('search')); $accessories = $accessories->TextSearch($request->input('search'));