0

I am trying to validate input quantity of an item in Laravel. How will I do this using request()->validate([])? I want to validate if the input quantity from user is greater than the quantity of item from database.

Also, is it possible to return an error message if the entered quantity is greater than the quantity from database?

Here's my validation, I know this is wrong.

public function deduct(Request $request)
{

    $item = Inventory::findOrFail($request->itemid);
    request()->validate([
        'quantity' => $item->quantity > $request->quantity
    ]);
}
kwestionable
  • 496
  • 2
  • 8
  • 23

2 Answers2

0

You can use min, max and digits_between

   $rules = ['test' => 'digits_between:2,5'];

or

   $rules = ['test' => 'numeric|min:2|max:5'];
mukesh kumar
  • 580
  • 3
  • 14
0

Set $item->quantity is the minimum value.

public function deduct(Request $request)
{

    $item = Inventory::findOrFail($request->itemid);
    $validatedData = $request->validate([
        'quantity' => 'min:'.(int)$item->quantity
    ]);

}
Rakibul Islam
  • 679
  • 9
  • 18