Position placeholder in validation messages

In Laravel 9 you can use the :position placeholder in validation messages if you're working with arrays.

This will output: "Please provide an amount for price #2"

class CreateProductRequest extends FormRequest
{
public function rules(): array
{
return [
'title' => ['required', 'string'];
'description' => ['nullable', 'sometimes', 'string'],
'prices' => ['required', 'array'],
'prices.*.amount' => ['required', 'numeric'],
'prices.*.expired_at' => ['required', 'date'],
];
}
 
public function messages(): array
{
'prices.*.amount.required' => 'Please provide an amount for price #:position'
}
}

Tip given by @mmartin_joo

Like our articles?

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

Recent New Courses