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
Enjoyed This Tip?
Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.
Recent Courses
[NEW] Laravel AI SDK: 6 Practical Examples
9 lessons
1 h 02 min
How to Build Laravel 12 API From Scratch
28 lessons
1 h 21 min
PhpStorm Junie AI for Laravel Projects: Crash Course
7 lessons
36 min