-2

When I am importing data from excel after getting the file and on the step of saving data to db it showing field doesn't have value from excel file. Where as all the fields are filled with data.

i am using laravel excel

LeadImport Code

 return new lead([
        'name'  => $row[0],
        'leadid'  => $row[1],
        'number'   => $row[2],
        'city'  => $row[3],
        'state' => $row[4],
        'address'   => $row[5],
        'pincode'   => $row[6],
        'type'  => $row[7],
        
    ]);

Controller Code
Excel::import(new LeadsImport, $request->file('file'));

Error

SQLSTATE[HY000]: General error: 1364 Field 'leadid' doesn't have a default value (SQL: insert into `leads` (`updated_at`, `created_at`) values (2022-01-14 19:24:21, 2022-01-14 19:24:21))
sagohi
  • 1
  • 2

2 Answers2

0

The leadid don't have any assigned value and it doesn't have any default value assigned. What is $row[1] returning? I assume row[1]'s value is null, that's why you actually get this error. dd() it and tell us what it returns.

  • if i set leadid to null then it showing same error on 'name' column – sagohi Jan 14 '22 at 14:09
  • `dd($row)` and see if it actually has any value in `row[0]` and `row[1]`. The error is coming from that you are trying to insert `literally nothing` in a table column. Or either to debug it make `name` and `leadid` nullable so you can see if it is actually inserting the other data. –  Jan 14 '22 at 14:11
  • leadid name number city state address pincode type 1123 vivek 8168501882 bhiwani haryana 221 127021 staff <<<< above is my data in excel. It is not print $row showing undefined variable..... also when i try print $request->file('file') in dd. It showing only file name and if i make leadid & name column null... then it will showing the same error on another column – sagohi Jan 14 '22 at 14:17
  • I think the problem is coming from `$request->file('file')`. It's not getting the file correct that's why everything is coming with a `null` error. I had similar problem with the `$request->file` but I can't remember where was the problem. Overall you have a problem with getting the file. Showing only the name of the file when `dd()` means you're not getting the file correct. –  Jan 14 '22 at 15:47
  • -test: false -originalName: "book.xlsx" -mimeType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" -error: 0 #hashName: null path: "C:\xampp\tmp" filename: "phpF2C2.tmp" basename: "phpF2C2.tmp" pathname: "C:\xampp\tmp\phpF2C2.tmp" extension: "tmp" realPath: "C:\xampp\tmp\phpF2C2.tmp" size: 8251 perms: 0100666 owner: 0 group: 0 type: "file" writable: true readable: true executable: false file: true dir: false link: false linkTarget: "C:\xampp\tmp\phpF2C2.tmp" <<<<<< dd response – sagohi Jan 15 '22 at 02:18
0

in your excel file you must have cells:

name, leadid, number, city, state, address, pincode, type

and in LeadImport just:

 return new lead([
    'name' => $row['name'],
    'leadid' => $row['leadid'],
    'number' => $row['number'],
    'city' => $row['city'],
    'state' => $row['state'],
    'address' => $row['address'],
    'pincode' => $row['pincode'],
    'type' => $row['type'],
    
]);
  • Thanks For giving you time...... when i am going through this. It's showing this new error 'Undefined index: leadid' – sagohi Jan 15 '22 at 02:44