abhikumar's avatar

SQLSTATE[HY000]: General error: 1364 Field 'company_id' doesn't have a default value

public function ImportUserData($filename,$company_id) { try {

        ini_set('max_execution_time', 2000);
        Excel::load($filename, function($reader) use($company_id) {
          //count total no of row in csv file
            $data = count($reader->all());
            //check count of row is not equal to 0 
            if($data==0){
                Session::flash('alert-danger', 'File does not content any data.');
                return Redirect::to(url('employee'));
            }
            else{
            $new_array = $reader->toArray();
            foreach($new_array as $key => $value) {
                $new_array[$key]['company_id'] = $company_id;
            }
              foreach ($new_array as $value) {
            if(!employee::where('name','=',!isset($value["name"])?'':trim($value["name"]))->where('is_deleted','=',0)->first()){
                 employee::updateOrCreate([
                      'Healthid' => $this->healthidGenerate(),
                      'name' => $value["name"],
                      'email' => $value["email"],
                      'phoneno' => $value["phoneno"],
                      'dob' => date("Y-m-d", strtotime($value["dob"])),
                      'gender' => $value["gender"],
                      'nationality' => $value["nationality"],
                      'religion' => $value["religion"],
                      'department' => $value["department"],
                      'company_id' => $value["company_id"]
                    ]);
                }
            }
        
            Session::flash('alert-success', 'File uploaded successfully');
                return Redirect::to(url('Company'));
         }
        });
      } catch (Exception $e) {
          Session::flash('error', $e->getMessage());
          return Redirect::to(url('employee/uploadData'))->withInput()->withErrors(["msg"=>$e->getMessage()]);
       }
}
0 likes
8 replies
topvillas's avatar

Yeah, Field 'company_id' doesn't have a default value

abhikumar's avatar

i had checked for company_id then only i am inserting data so i don't neet default value for company_id column

topvillas's avatar

You do need a default value if you're not supplying one and the column isn't nullable.

abhikumar's avatar

but my company_id is containing value for each row i can show you array if you need

Snapey's avatar

You possibly have an empty row in the import data. Check your data. It is likely to be the last row?

Easy to add a check that the mandatory fields are present?

abhikumar's avatar

Array ( [0] => Array ( [name] => aaa [email] => [email protected] [phoneno] => 4567891230 [dob] => 11/10/1993 [gender] => f [nationality] => indian [religion] => hindu [department] => It [company_id] => 1 )

[1] => Array
    (
        [name] => bbbb
        [email] => [email protected]
        [phoneno] => 4545746545
        [dob] => 31/11/2016
        [gender] => m
        [nationality] => indian
        [religion] => hindu
        [department] => hr
        [company_id] => 1
    )

[2] => Array
    (
        [name] => ccc
        [email] => [email protected]
        [phoneno] => 45678678465
        [dob] => 21/12/2016
        [gender] => m
        [nationality] => indian
        [religion] => hindu
        [department] => developer
        [company_id] => 1
    )

[3] => Array
    (
        [name] => ddd
        [email] => [email protected]
        [phoneno] => 786978678
        [dob] => 11/3/2016
        [gender] => f
        [nationality] => indian
        [religion] => hindu
        [department] => finance
        [company_id] => 1
    )

[4] => Array
    (
        [name] => fff
        [email] => [email protected]
        [phoneno] => 6786786
        [dob] => 1/4/2016
        [gender] => f
        [nationality] => indian
        [religion] => hindu
        [department] => it
        [company_id] => 1
    )

[5] => Array
    (
        [name] => uu
        [email] => [email protected]
        [phoneno] => 77865246
        [dob] => 1/5/2016
        [gender] => m
        [nationality] => indian
        [religion] => hindu
        [department] => it
        [company_id] => 1
    )

[6] => Array
    (
        [name] => eeee
        [email] => [email protected]
        [phoneno] => 576545
        [dob] => 1/6/2016
        [gender] => f
        [nationality] => kfjdsg
        [religion] => gfdg
        [department] => gfdg
        [company_id] => 1
    )

)

Snapey's avatar

Why do you have nested foreach?

try dump($value) within your inner loop and see how many iterations you get before failure

Please or to participate in this conversation.