mercredi 19 décembre 2018

Laravel: Importing Null Values in the database table from CSV file Upload

I am trying to upload data from a csv file. When the data is imported to the database table, the values are NULL. My CSV file has value.

I did this process by installing Maatwebsite\Excel.

my code

  public function upload(Request $request)
    {
        if ($request->hasFile('uploadRates')) {
            $path = $request->file('uploadRates')->getRealPath();
            $data = \Excel::load($path)->get();

            if (!empty($data) && $data->count()) {
                foreach ($data as   $value) {
                    $arr[] = ['Origin' => $value->Origin,
                              'OriginState' => $value->OriginState,
                              'Destination' => $value->Destination,
                              'DestinationState' => $value->DestinationState,
                              'Carrier' => $value->Carrier,
                              'Car' => $value->Car,
                              'Buy' => $value->Buy,
                              'Carbon' => $value->Carbon,
                              'DepotFrom' => $value->DepotFrom,
                              'DepotTo' => $value->DepotTo,
                              'OriginType' => $value->OriginType,
                              'DestinationType' => $value->DestinationType,
                              'GoodsAllowed' => $value->GoodsAllowed,
                              'NonRunners' => $value->NonRunners,
                              'ETA' => $value->ETA,
                              'Comments' => $value->Comments,
                              'Disabled' => $value->Disabled,

                             ];
                }
                if (!empty($arr)) {
                    DB::table('template')->insert($arr);
                    return "Success";
                }
            }
        }
        dd('Requested file has no Data inside.');
    }

and my CSV data:

Origin,OriginState,Destination,DestinationState,Carrier,Car,Buy,Carbon,DepotFrom,DepotTo,OriginType,DestinationType,GoodsAllowed,NonRunners,ETA,Comments,Disabled

Adelaide,SA,Melbourne,VIC,A1 Auto Transporter,Car,250,0,0,0,Depot,Depot,0,0,5,,0 Adelaide,SA,Canberra,ACT,A1 Auto Transporter,Car,609.0909091,0,0,0,Depot,Depot,0,0,7,,0 Adelaide,SA,Sydney,NSW,A1 Auto Transporter,Car,409.0909091,0,0,0,Depot,Depot,0,0,7,,0 Adelaide,SA,Brisbane,QLD,A1 Auto Transporter,Car,727.2727273,0,0,0,Depot,Depot,0,0,8,,0 Adelaide,SA,Townsville,QLD,A1 Auto Transporter,Car,1428.181818,0,0,0,Depot,Depot,0,0,12,,0 Adelaide,SA,Cairns,QLD,A1 Auto Transporter,Car,1491.818182,0,0,0,Depot,Depot,0,0,14,,0

my database screeshot: enter image description here



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire