违反诚信约束:1048专栏'电话'不能为空 - 拉拉维尔

时间:2017-12-12 11:10:53

标签: php laravel maatwebsite-excel

将excel文件上传到我的数据库时,我收到此错误,因为需要手机字段。当我上传一个包含所需标题(行)的文件时,文件上传成功,但是当我上传一个没有代码所需行的文件时,我收到此错误 Integrity constraint violation: 1048 Column 'phone' cannot be null - Laravel

有没有办法可以检查行,以便处理错误?

 if($request->file('imported-file'))
    {
              $path = $request->file('imported-file')->getRealPath();

              $data = Excel::load($path, function($reader)
              {
                 $reader->calculate(false);

              })->get();

        if(($request->file('imported-file')->getClientOriginalExtension()) != 'xlsx')
        {
            return redirect('/')->with('error','File Format may not be supported');

        }
        else {
            if(!empty($data) && $data->count())
            {
              foreach ($data->toArray() as $row)
              {
                if(!empty($row))
                {
                  $dataArray[] =
                  [
                    'name' => array_get($row, 'name'),
                    'phone' => array_get($row, 'phone'),
                  ];
                }

            }
            if(!empty($dataArray))
            {
               People::insert($dataArray);

               return redirect('')->with('status','Clients successfully added');
             }
           }
        }     
 }
 else {
    return redirect('/')->with('error','No file was uploaded. Please upload a file');

 }

}  

0 个答案:

没有答案