2

I have laravel 5.2, i want to validate the csv upload, let's say i have an array from uploaded csv like this :

array(
    'code' => '1',
    'name' => 'asd'
)

and my validation like this

rules = [
    'code' => 'required|numeric',
    'name' => 'required'
];

but i used a locale for my laravel project, so when i export to csv, the title will not alway code and name, i could be japanese language or others, so how to create my validation rule?

How to validate each row from index number?

foreach( $uploaded_data as $row ) {
    $rules = [
        0 => 'required|numeric',
        1 => 'required'
    ];
}
2

1 Answer 1

3

Using the Laravel 5.2 Collection class you can call combine() to map an array of 'keys' to a second array.

See the documentation for the combine method.

First we get the keys of the $rules, then combine with the data.

$rules = [
    'code' => 'required|numeric',
    'name' => 'required'
];

$data = [
    'foo' => '1',
    'bar' => 'asd'
];

$combined = collect($rules)->keys()->combine($data);
// [
//     "code" => 1,
//     "name" => 'asd'
// ]

Then, just call validate on $combined.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.