Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current index in a array-validation

I got an input that contains an array of objects. Now I'd like to validate it, but one part of the validation depends on another item of the object, so I need it's index:

[
    'items.myarray.*.owner_id' => 'numeric|exists:App\Models\Owner',
    'items.myarray.*.key'      => ['nullable', 'string', 'min:20', 'max:20', 'exists:App\Models\Invitation,key',
                           new KeyIsValid(Owner::findOrFail(request()->input('items.*.owner_id')))],
];

Of course request()->input('items.myarray.*.owner_id') is not working - but I am looking for a way to pass the owner_id of the current object to this validation.

like image 286
SPQRInc Avatar asked May 22 '26 01:05

SPQRInc


1 Answers

This will return an array value Try to use

$request()->input('items.myarray.*.key')[0]

it will return a string value of "items.myarray.*.key" and it should work.

like image 79
Jaydeep Khokhar Avatar answered May 24 '26 18:05

Jaydeep Khokhar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!