I have an $array
with approx. 100k+ entries.
I convert the array into a collection using this code:
$insert_data = collect($array);
// Make a collection to use the chunk method
$chunks = $insert_data->chunk(10000);
which will create 10 bunches of 10000 records.
Now I want to validate the fields, so I use this code:
$rules2 = [
'email.*' => 'required|email|max:255|unique:users,email',
'phone.*' => 'required|min:10|unique:users,phone',
'password.*' => 'required',
];
$rmsgs = [
'email.unique' => 'The email has already been taken.',
'phone.unique' => 'The phone has already been taken.',
];
$validator = Validator::make($array, $rules2, $rmsgs);
if ($validator->fails()) {
$m = [];
$m['message'] = '';
$xx = $validator->errors();
foreach ($xx->all() as $message) {
$m['message'] .= $message;
$m['message'] .= ',';
}
$m['message'] = rtrim($m['message'], ",");
array_push($msg, $m);
continue;
}
DB::table('users')->insert($chunk->toArray());
Now this validation does not work while importing the file but it simply does not throw any error in $msg
array.
what am I doing wrong? I left email address empty into CSV for some records but it does not return any error.
You have to do that
$rules2 = [
'*.email' => 'required|email|max:255|unique:users,email',
'*.phone' => 'required|min:10|unique:users,phone',
'*.password' => 'required'
];
and you must do it in cron job