"[email protected]" "role_id" => 2 ] 1 => a" /> "[email protected]" "role_id" => 2 ] 1 => a" /> "[email protected]" "role_id" => 2 ] 1 => a"/>

How do I validate email in array laravel

280 views Asked by At

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

array:2 [
  0 => array:2 [
    "email" => "[email protected]"
    "role_id" => 2
  ]
  1 => array:2 [
    "email" => "[email protected]"
    "role_id" => 3
  ]
]

How do I validate the email and role_id in laravel

1

There are 1 answers

0
Madan Sapkota On

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.