Laravel规则验证最低和最高工资[重复]

This question already has an answer here:

I have the following rules.

  $rules['min_salary']  = 'required|not_in:0|numeric';
  $rules['max_salary']  = 'required|not_in:0|numeric';

How to have the rule that min_salary is less than or equal to max_salary.

</div>

Create a new rule

php artisan make:rule SalaryRule

Add the rule to your validation

$rules['min_salary']  = ['required', 'not_in:0', 'numeric', new SalaryRule($request->max_salary)];
$rules['max_salary']  = 'required|not_in:0|numeric';

If you are using a FormRequest you could access max_salary as $this->max_salary

Make sure to import the rule in your code

use App\Rules\SalaryRule;

and in the passes method of the Rule you could write your logic

public function passes($attribute, $value)
{
    return $value <= $this->max_salary;
}

and create a property max_salary and initialize it in the rule's constructor method as you are passing the max_salary in the constructor.

protected $max_salary;

public function __construct($max_salary)
{
    $this->max_salary = $max_salary;
}