Laravel - Validation - Complex Conditional Array Validation

Sometimes you may want to validate a field based on another field in the same nested array whose index you do not know. In these situations, you may allow your closure to receive a second argument which will be the current individual item in the array being validated:

    
    $input = [
        'channels' => [
            [
                'type' => 'email',
                'address' => '[email protected]',
            ],
            [
                'type' => 'url',
                'address' => 'https://example.com',
            ],
        ],
    ];
    
    $validator->sometimes('channels.*.address', 'email', function($input, $item) {
        return $item->type === 'email';
    });
    
    $validator->sometimes('channels.*.address', 'url', function($input, $item) {
        return $item->type !== 'email';
    });
	

Like the $input parameter passed to the closure, the $item parameter is an instance of Illuminate\Support\Fluent when the attribute data is an array; otherwise, it is a string.