php 如何修复在Laravel中从CSV保存数据时的“无效日期时间格式”错误?

bn31dyow  于 2023-05-27  发布在  PHP
关注(0)|答案(1)|浏览(229)

从CSV文件保存数据并更新数据库时发生此错误。
无效的日期时间格式:7错误:类型date的输入语法无效:“”上下文:未命名门户参数$6 = ''(SQL:更新“crew_members”set“cdcno”=,“passportno”=,“birth_date”= 01-01-2005,“birth_city”=,“cdc_issued_at”=,“cdc_issued_on”=,“cdc_expires_on”=,“passport_issued_at”=,“passport_issued_on”=,“passport_expires_date”=,“presea_cert”=,“last_dna_date”=,“address_medical_center”=,“updated_at”= 2023-05-25 12:52:13 where“id”= 49)
我不想要错误的日期时间。

控制器

public function crewupdatecsv(Request $request)
{
    $file = $request->file('updated_crew');
    //dd($file);
    if (strtolower($file->getClientOriginalExtension()) == 'csv') {
        $fh = fopen($file, 'r');
        $header = fgetcsv($fh);

        while ($data = fgetcsv($fh)) {                
            //dd(request()->all());
            $crewmember = CrewMember::where('id', $data[1])->first();
            //dd($crewmember);
           // $update['company_id'] = $data[2];
            $update['last_vessel'] = $data[3];
            $update['first_name'] = $data[4];
            $update['middle_name'] = $data[5];
            $update['last_name'] = $data[6];
            $update['gender'] = $data[7];
            $update['rank_id'] = $data[8];
            $update['nationality'] = $data[9];
            $update['birth_date'] = $data[10];
            $update['birth_city'] = $data[11];
            $update['cdcno'] = $data[12];
            $update['cdc_issued_at'] = $data[13];
            $update['cdc_issued_on'] = $data[14];
            $update['cdc_expires_on'] = $data[15];
            $update['passportno'] = $data[16];
            $update['passport_issued_at'] = $data[17];
            $update['passport_issued_on'] = $data[18];
            $update['passport_expires_date'] = $data[19];
            $update['presea_cert'] = $data[20];
            $update['last_dna_date'] = $data[21];
            $update['last_dna_test_result'] = $data[22];
            $update['address_medical_center'] = $data[23];
            //dd($update);
            $crewmember->update($update);
            
        }
        
        return back()->with('success', 'Bulk Crew Member are updated.');
    } else
        return back()->with('failure', 'Wrong type of file is uploaded.');
   
}

刀锋

@foreach ($crewmembers as $i => $cm)
<tr>
    <td>{{$cm->id}}</td>
    <td class="standard_grid_table_yellow">{{$cm['company']['company_name']??''}}</td>
    <td class=""><input type="text" name="last_vessel" value="{{$cm['vessel']['vessel_name']??''}}"> </td>
    <td class=""><input type="text" name="first_name" value=" {{$cm->first_name}}"></td>
    <td class="standard_grid_table_yellow"><input type="text" name="middle_name" value="{{$cm->middle_name}}"></td>
    <td class=""><input type="text" name="last_name" value="{{$cm->last_name}}"></td>
    <td class="standard_grid_table_yellow"><input type="text" name="gender" value="{{$cm->gender}}"></td>
    <td class=""><input type="text" name="rank_id" value="{{$cm['rank']['rank_name']??''}}"></td>
    <td class="standard_grid_table_yellow"><input type="text" name="nationality" value="{{$cm['country']['country_name']}}"></td>
    <td class=""><input type="text" name="birth_date" class="dateentry" value="{{$cm->birth_date}}"></td>
    <td class="standard_grid_table_yellow"><input type="text" name="birth_city" value="{{$cm->birth_city }}"></td>
    <td class=""><input type="text" name="cdcno" value="{{$cm->cdcno}}"></td>
    <td class="standard_grid_table_yellow"> <input type="text" name="cdc_issued_at" value="{{$cm->cdc_issued_at}}"></td>
    <td class=""><input type="text" name="cdc_issued_on" class="dateentry" value="{{$cm->cdc_issued_on }}"></td>
    <td class="standard_grid_table_yellow"><input type="text" class="dateentry" name="cdc_expires_on" value="{{$cm->cdc_expires_on}}"></td>
    <td class=""><input type="text" name="passportno" value="{{$cm->passportno}}"></td>
    <td class="standard_grid_table_yellow"><input type="text" name="passport_issued_at" value="{{$cm->passport_issued_at}}"></td>
    <td class=""><input type="text" name="passport_issued_on" class="dateentry" value="{{$cm->passport_issued_on}}"></td>
    <td class="standard_grid_table_yellow"><input type="text" name="passport_expires_date" class="dateentry" value="{{$cm->passport_expires_date}}"></td>
    <td class=""><input type="text" name="presea_cert" value="{{$cm->presea_cert}}"></td>
    <td class="standard_grid_table_yellow"><input type="text" name="last_dna_date" value="{{$cm->last_dna_date}}"></td>
    <td class=""><input type="text" name="last_dna_test_result" class="dateentry" value="{{$cm->last_dna_test_result}}"></td>
    <td class="standard_grid_table_yellow"><input type="text" name="address_medical_center" value="{{$cm['centre']['centre_name']??''}}"></td>
</tr>
@endforeach
tf7tbtn2

tf7tbtn21#

在您的数据库中,字段birth_datedatetimetimestamp(并且需要是YYYY-MM-DD HH::MM::SS)或仅是date(并且需要是YYYY-MM-DD),因此,您应该使用正确的格式插入。您可以使用carbon并执行以下操作:

use Carbon\Carbon;

//code...
while ($data = fgetcsv($fh)) {
    //code...
    $update['birth_date'] = Carbon::createFromFormat('d-m-Y', $data[10])->format('Y-m-d H:i:s'); //for `date` type use ->format('Y-m-d');
}
//code...

相关问题