Laravel: Validating a number greater than zero is failing Laravel: Validating a number greater than zero is failing laravel laravel

Laravel: Validating a number greater than zero is failing


gt, gte, lt and lte are added in Laravel 5.6 and later versions, I'm guessing that must be the reason for you get the error. (It's working for me though.)

I think you can try like this

$request->validate([    'product_price' => 'required|numeric|min:0|not_in:0',]);

min:0 make sure the minimum value is 0 and no negative values are allowed. not_in:0 make sure value cannot be 0. So, combination of both of these rules does the job.

You can define meaningful error messages for certain rule. (You can achieve the same result using regular expressions as well.)


You can try this way ,

Before invoking the Validator::make() function, modify the set of rules by appending the value to compare to like so:

use Illuminate\Support\Facades\Validator;Validator::extend('greater_than', function ($attribute, $value, $otherValue) {      return intval($value) > intval($otherValue[0]);});$validation = Validator::make($input, ['amount' => 'required|numeric|greater_than:0']);


For me, this code is working in my project.

$validation_rules = array(                 'user_id' => 'required|integer|gt:0',                'type_id' => 'required|integer|gt:0',            );$validation = Validator::make($request->all(), $validation_rules);

Here, gt:0 check if the integer is greater than zero.

Hope, this will work for you. If not then please check your Laravel version.