I don't see you save any model or date field... Not sure what your question is...
Feb 23, 2016
6
Level 5
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));
}
}
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.