Array elements validation

If you want to validate elements of an array that you submited use dot notation in rules with '*'

1// say you have this array
2// array in request 'user_info'
3$request->validated()->user_info = [
4 [
5 'name' => 'Qasim',
6 'age' => 26,
7 ],
8 [
9 'name' => 'Ahmed',
10 'age' => 23,
11 ],
12];
13 
14// Rule
15$rules = [
16 'user_info.*.name' => ['required', 'alpha'],
17 'user_info.*.age' => ['required', 'numeric'],
18];

Tip given by HydroMoon

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord