Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Hamelraj's avatar

Couldn't insert DATE when i import CSV file ?

i import CSV file to our database but when everything works perfect but this date columns arrievd sailed its saving in my database like 0000-00-00 this

{!! Form::open(['url' => ['/vessels/post'],'files'=>true, 'method'=>'POST','class'=>'form-horizontal','role'=>'form']) !!}
{!! csrf_field() !!}
        <input type="file" class="form-control " name="file">
        <button type="submit" value="submit"class="btn btn-primary"><i class="fa fa-btn fa-user"></i>Upload</button>

{!! Form::close() !!}
private function _import_csv($path, $filename)
{
    $csv = $path . $filename;
    $query = sprintf("LOAD DATA local INFILE '%s' INTO TABLE vessels FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\"' LINES TERMINATED BY '\\n' IGNORE 1 LINES (`job`, `name`,`port`,`arrived`,`sailed`,`principal`,`launch`,`purpose`,`pda`,`advance_pda`,`fda`,`passed`,`sent`,`fda_amount`,`fda_status`,`call_id`,`created_at`,`updated_at`)", addslashes($csv));
    return DB::connection()->getpdo()->exec($query);

}
public function postUpload (Request $request)
{
    if ($request->hasFile('file')){

        $file = $request->file('file');
        $name = time() . '-' . $file->getClientOriginalName();
        $uploadDestinationPath = base_path() . '/assets/documents/';
        $file->move($uploadDestinationPath, $name);
        return ( $this->_import_csv($uploadDestinationPath, $name) ? 'OK' : 'No rows affected' );
       //dd($this->_import_csv($uploadDestinationPath, $name));

    }

}
0 likes
6 replies
bobbybouwmann's avatar

I don't see you save any model or date field... Not sure what your question is...

1 like
Hamelraj's avatar

@bobbybouwmann im inserting data in to my table call **vessels ** by uploading CSV file ok . its working perfect in that sheet has two columns DATE FORMAT when i upload CSV file those two columns all are **0000-00-00** you got it here is table and columns name

$query = sprintf("LOAD DATA local INFILE '%s' INTO TABLE vessels FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\"' LINES TERMINATED BY '\\n' IGNORE 1 LINES (`job`, `name`,`port`,`arrived`,`sailed`,`principal`,`launch`,`purpose`,`pda`,`advance_pda`,`fda`,`passed`,`sent`,`fda_amount`,`fda_status`,`call_id`,`created_at`,`updated_at`)", addslashes($csv));
bobbybouwmann's avatar

You need to show use the _import_csv function.. We can't just guess what's going on in your code! Just trying to help here, but if you provide zero information and no context how am I suppose to help you?

1 like
Hamelraj's avatar

@bobbybouwmann you can check it is there brother :( else check below

private function _import_csv($path, $filename)
{
    $csv = $path . $filename;
    $query = sprintf("LOAD DATA local INFILE '%s' INTO TABLE vessels FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\"' LINES TERMINATED BY '\\n' IGNORE 1 LINES (`job`, `name`,`port`,`arrived`,`sailed`,`principal`,`launch`,`purpose`,`pda`,`advance_pda`,`fda`,`passed`,`sent`,`fda_amount`,`fda_status`,`call_id`,`created_at`,`updated_at`)", addslashes($csv));
    return DB::connection()->getpdo()->exec($query);

}
bobbybouwmann's avatar
Level 88

You probably have a different format in your csv file then Laravel is expecting. Can you paste one row in your csv file here so we can see it?

1 like

Please or to participate in this conversation.