Support the ongoing development of Laravel.io →
posted 7 years ago
Validation
Last updated 1 year ago.
0
Solution

in your request validation, you can query to get the value you want to check against. so if you had a table that stores availability for a product, you can do the following.

$product_availability = ProductAvailability::select('available_quantity')->where('product_id', $request->product_id)->first();
$rules['quantity_sold'] = 'max:'.$product_availability->available_quantity;

in the example you query the db to get the available quantity of the product. in the validation, the maximum number accepted should not be higher than that value. i don't know how your db is set up or how you calculate the available quantity but i hope this gives you an idea.

0

Thanks, it worked after a small modification

$rules = array( 'product_id' =>'required', 'quantity_sold' =>'max:'.$product_availability, );

0

w1n78 said:

in your request validation, you can query to get the value you want to check against. so if you had a table that stores availability for a product, you can do the following.

$product_availability = ProductAvailability::select('available_quantity')->where('product_id', $request->product_id)->first();
$rules['quantity_sold'] = 'max:'.$product_availability->available_quantity;

in the example you query the db to get the available quantity of the product. in the validation, the maximum number accepted should not be higher than that value. i don't know how your db is set up or how you calculate the available quantity but i hope this gives you an idea.

It stopped working, any ideas why?

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Gertie1 gertie1 Joined 28 Oct 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.