I am trying to import users in my database. My code .
public function staffImport(Request $request)
{
$this->validate($request, array(
'file' => 'required'
));
if ($request->hasFile('file')) {
$extension = File::extension($request->file->getClientOriginalName());
if ($extension == "xlsx" || $extension == "xls" || $extension == "csv") {
$path = $request->file->getRealPath();
$spreadsheet = IOFactory::load($path);
$sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
// dd($sheetData);
if (!empty($sheetData)) {
$startFrom = 2;
$row_header = $sheetData[1];
$endAt = count($sheetData);
$count = 1;
$error_msg = array();
$updated_msg = array();
$created_msg = array();
$sendEmail = 'yes';
foreach ($sheetData as $key => $value) {
if ($key >= $startFrom && $key <= $endAt) {
if (array_filter($value)) {
$excel_row = array_combine($row_header, $value);
$flag = true;
$name = $excel_row['name'];
$email = $excel_row['email'];
$social_security_number = $excel_row['Social Security Number'];
$password = '12345678';
$status = $excel_row['state'];
$roleId = $excel_row['role_id'];
// dd($excel_row);
if (empty($name)) {
$error_msg[$count++] = 'Name provided in row ' . ($key + 1) . ' empty';
$flag = false;
}
if (empty($email)) {
$error_msg[$count++] = 'Email provided in row ' . ($key + 1) . ' empty';
$flag = false;
}
if (empty($password)) {
$error_msg[$count++] = 'Password provided in row ' . ($key + 1) . ' empty';
$flag = false;
} else {
$password = Hash::make($password);
}
if ($roleId == 10 && empty($excel_row['address']) || empty($excel_row['longitude']) || empty($excel_row['latitude']) || empty($excel_row['brand']) || empty($excel_row['body_type']) || empty($excel_row['year_of_truck']) || empty($excel_row['type_of_ground']) || empty($excel_row['plate_number']) || empty($excel_row['color']) || empty($excel_row['owner_name']) || empty($excel_row['tax_id']) || empty($excel_row['driver_license_number']) || empty($excel_row['driver_license_type'])) {
// var_dump('empty');
$error_msg[$count++] = 'Data provided in row for ' . ($key + 1) . ' empty';
$flag = false;
}
if (empty($status)) {
$status = 0;
} elseif ($status == 'Active') {
$status = 1;
} elseif ($status == 'Inactive') {
$status = 0;
} else {
$status = 0;
}
if ($flag) {
$commonDetails = [
'name' => $name,
'email' => $email,
'password' => $password,
'social_security_number' => $social_security_number,
'status' => $status,
'admin' => 0,
];
if (!Staff::where('email', $commonDetails['email'])->exists()) {
if (!Staff::where('social_security_number', $commonDetails['social_security_number'])->exists()) {
$staff = Staff::create($commonDetails);
$this->createUserRecordOnImport($roleId, $staff, $excel_row);
if (isset($roleId)) {
DB::insert('insert into staff_role (role_id, staff_id) values (?, ?)', [$roleId, $staff->id]);
$role = Role::find($roleId);
}
$created_msg[$count++] = $staff;
} else {
$error_msg[$count++] = 'Social Security Number provided in row ' . ($key + 1) . ' already exists';
}
} else {
$error_msg[$count++] = 'Email provided in row ' . ($key + 1) . ' already exists';
}
}
}
}
}
return view('web.admin.pages.staffs.import_staff_validation', compact('error_msg', 'created_msg'));
}
return back()->with('error', 'There is no data in file');
} else {
return back()->with('error', 'You are uploading a ' . $extension . ' file.!! Please upload a valid xls/xlsx/csv file..!!');
}
}
}
My file contains data for seven new user out of which only one user has roleId of 10 but I get error 7 times. I don't know why this condition get truthy 7 times. it should give error only one time. is there anything wrong with my code or something? Any thing on that?
Aucun commentaire:
Enregistrer un commentaire