1

I am sending data request in this format to my laravel backend application

array:2 [
  0 => array:2 [
    "email" => "sam@gmail.com"
    "role_id" => 2
  ]
  1 => array:2 [
    "email" => "joy@gmail.com"
    "role_id" => 3
  ]
]

How do I validate the email and role_id in laravel

Madan Sapkota
  • 25,047
  • 11
  • 113
  • 117
etranz
  • 891
  • 2
  • 10
  • 29

1 Answers1

1

If your validation is generated using php artisan make:request use the rules method.

/**
 * Get the validation rules that apply to the request.
 * @return array
 */
public function rules(): array
{
    return [
        'array_name' => ['required', 'array'],
        'array_name.*.email' => ['required', 'email'],
        'array_name.*.role_id' => ['required', '...'],
    ];
}

The same validation rules can be applied within the controller validate method.

Madan Sapkota
  • 25,047
  • 11
  • 113
  • 117