lundi 2 avril 2018

Array to string conversion - Laravel 5.6 Error

I am trying to update values in the DB using values from a JSON file:

Code:

$jsonData = file_get_contents($jsonFile);
        $data = json_decode($jsonData, true);

        //check if hospital exist
        $name = explode(' ',trim($data['organisationUnits']['organisationUnit']['name']));
        // echo $name[0];
        $query = Hospital::where('h_name',  'LIKE' , '%' . $data['organisationUnits']['organisationUnit']['name'] . '%')->first();

        if($query){

            // echo "\n yupo";
            $h_id = $query->id;
            $h_slug = $query->h_slug;
            $nr_orgUnit = $query->nr_orgUnit;

            // echo $nr_orgUnit;
            $updateHospital = Hospital::find($h_id);
            $updateHospital->h_name = $data["organisationUnits"]["organisationUnit"]["name"];
            $updateHospital->h_short_name = $data["organisationUnits"]["organisationUnit"]["shortName"];
            $updateHospital->h_code = $data["organisationUnits"]["organisationUnit"]["code"];
            $updateHospital->h_opening_date = $data["organisationUnits"]["organisationUnit"]["openingDate"];
            $updateHospital->h_closed_date = $data["organisationUnits"]["organisationUnit"]["closedDate"];
            $updateHospital->h_active = $data["organisationUnits"]["organisationUnit"]["active"];
            $updateHospital->h_comment = $data["organisationUnits"]["organisationUnit"]["comment"];
            $updateHospital->h_geo_code = $data["organisationUnits"]["organisationUnit"]["geoCode"];
            $updateHospital->h_last_updated = $data["organisationUnits"]["organisationUnit"]["lastUpdated"];
            $updateHospital->save();
} else {
            // echo 'error';
        }

JSON DATA:

 {"organisationUnits":{
  "organisationUnit":{
     "id":"01",
     "uuid":{

     },
     "name":"Isagehe Dispensary",
     "shortName":"Isagehe Dispensary ",
     "code":"17-04-0118",
     "openingDate":"1990-01-01",
     "closedDate":{

     },
     "active":"true",
     "comment":{

     },
     "geoCode":{

     },
     "lastUpdated":{

     }
  }
}
}

when i try to run the code, i get the following error:

Array to string conversion (SQL: update `ag_hospitals` set `h_closed_date` = , `h_active` = true, `h_comment` = , `h_geo_code` = , `h_last_updated` = where `id` = 41)"

where might i be wrong?

Note i have also tried updating the following way:

$updateHospital = Hospital::where('id', $h_id)->update([
                                'h_name' => $data['organisationUnits']['organisationUnit']['name'], 
                                'h_short_name' => $data['organisationUnits']['organisationUnit']['shortName'], 
                                'h_code' => $data['organisationUnits']['organisationUnit']['code'], 
                                'h_opening_date' => $data['organisationUnits']['organisationUnit']['openingDate'], 
                                'h_closed_date' => $data['organisationUnits']['organisationUnit']['closedDate'], 
                                'h_active' => $data['organisationUnits']['organisationUnit']['active'], 
                                'h_comment' => $data['organisationUnits']['organisationUnit']['comment'], 
                                'h_geo_code' => $data['organisationUnits']['organisationUnit']['geoCode'], 
                                'h_last_updated' => $data['organisationUnits']['organisationUnit']['lastUpdated']
                            ]);



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire