Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Laravel中的foreach()循环中使用if语句_Php_Laravel - Fatal编程技术网

Php 如何在Laravel中的foreach()循环中使用if语句

Php 如何在Laravel中的foreach()循环中使用if语句,php,laravel,Php,Laravel,我正在尝试导入数据库中的用户 public function staffImport(Request $request) { $this->validate($request, array( 'file' => 'required' )); if ($request->hasFile('file')) { $extension = File::extension($r

我正在尝试导入数据库中的用户

 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..!!');
            }
        }
    }
公共函数staffImport(请求$Request)
{
$this->validate($request,array)(
'文件'=>'必需'
));
如果($request->hasFile('file')){
$extension=File::extension($request->File->getClientOriginalName());
如果($extension==“xlsx”| |$extension==“xls”| |$extension==“csv”){
$path=$request->file->getRealPath();
$spreadsheet=IOFactory::load($path);
$sheetData=$spreadsheet->getActiveSheet()->toArray(null、true、true、true);
//dd(数据);
如果(!空($sheetData)){
$startFrom=2;
$row_header=$sheetData[1];
$endAt=计数($sheetData);
$count=1;
$error_msg=array();
$updated_msg=array();
$created_msg=array();
$sendmail='yes';
foreach($sheetData作为$key=>$value){
如果($key>=$startFrom&&$key$name),
“email”=>$email,
“密码”=>$password,
“社会保障号码”=>$social\u security\u号码,
“状态”=>$status,
“admin”=>0,
];
如果(!Staff::where('email',$commonDetails['email'])->存在(){
如果(!Staff::where('social_security_number',$commonDetails['social_security_number'])->存在(){
$staff=staff::create($commonDetails);
$this->createUserRecordOnImport($roleId、$staff、$excel\u行);
if(isset($roleId)){
DB::insert('insert into staff_role(role_id,staff_id)值(?,),[$roleId,$staff->id]);
$role=role::find($roleId);
}
$created_msg[$count++]=$staff;
}否则{
$error_msg[$count++]=“第行提供的社会保险号码”。($key+1)。“已存在”;
}
}否则{
$error_msg[$count++]=“第行提供了电子邮件”。($key+1)。“已存在”;
}
}
}
}
}
返回视图('web.admin.pages.staff.import_staff_validation',compact('error_msg','created_msg');
}
return back()->带有('error','file中没有数据');
}否则{
return back()->带有('error',您正在上载一个'.$扩展名。!!请上载一个有效的xls/xlsx/csv文件。!!);
}
}
}

我的文件包含七个新用户的数据,其中只有一个用户的
roleId
为10,但我出现了7次错误。我不知道为什么这种情况会出现7次错误。它应该只出现一次错误。我的代码或其他东西有什么问题吗?

你确定错误的原因是
$roleId
?你也是tes吗“我认为错误的原因是我在foreach循环中使用了if条件,所以总共有7个条目和7个错误。如果我有2个用户的角色为_id 2,那么总共有14个错误。您的代码几乎不可读,一般来说,if statem需要的每个缩进。”ents循环等使它更加混乱。