php - Laravel - Creating an object from a JSON array to save it in a SQL database -


what trying send json array (that gotten guzzle) sql database. have gotten point able response , display gotten json array on webpage. array defined $data variable. $data variable gets decoded using this:

$data = json_decode($response->getbody()->getcontents()); 

this able json , decode no problem. part stuck on taking $data variable, processing , sending database. understand required convert json array , send database.

the json format this:

[{   "intldes": "2017-042z",   "norad_cat_id": "42848",   "object_type": "tba",   "satname": "object z",   "country": "tbd",   "launch": "2017-07-14",   "site": "ttmtr",   "decay": null,   "period": "96.52",   "inclination": "97.61",   "apogee": "597",   "perigee": "586",   "comment": null,   "commentcode": null,   "rcsvalue": "0",   "rcs_size": null,   "file": "6242",   "launch_year": "2017",   "launch_num": "42",   "launch_piece": "z",   "current": "y",   "object_name": "object z",   "object_id": "2017-042z",   "object_number": "42848" }] 

my satellite model goes this:

protected $fillable = [     'intldes',     'norad_cat_id',     'object_type',     'satname',     'country',     'launch',     'site',     'decay',     'period',     'inclination',     'apogee',     'perigee',     'comment',     'commentcode',     'rcsvalue',     'rcs_size',     'file',     'launch_year',     'launch_num',     'launch_piece',     'current',     'object_name',     'object_id',     'object_number' ];  

my migrations file:

schema::create('satellites', function (blueprint $table) {     $table->increments('id');     $table->string('intldes');     $table->string('norad_cat_id');     $table->string('object_type');     $table->string('satname');     $table->string('country');     $table->string('launch')->nullable();     $table->string('site')->nullable();     $table->string('decay')->nullable();     $table->string('period')->nullable();     $table->string('inclination')->nullable();     $table->string('apogee')->nullable();     $table->string('perigee')->nullable();     $table->string('comment')->nullable();     $table->string('commentcode')->nullable();     $table->string('rcsvalue')->nullable();     $table->string('rcs_size')->nullable();     $table->string('file')->nullable();     $table->string('launch_year')->nullable();     $table->string('launch_num')->nullable();     $table->string('launch_piece')->nullable();     $table->string('current')->nullable();     $table->string('object_name');     $table->string('object_id');     $table->string('object_number');     $table->timestamps(); }); 

i tried making $object array, did not work.

tl;dr: want take $data variable, contains decoded json , create allows saved 'satellites' sql database.

edit: here full satellite controller:

    public function displayer(){     $api = new client([     'base_uri' => 'https://www.space-track.org',     'cookies' => true,      ]); $api->post('ajaxauth/login', [       'form_params' => [          'identity' => '#',           'password' => '#',       ],     ]);     $response = $api->get('basicspacedata/query/class/satcat/orderby/intldes%20desc/limit/5/metadata/false');     $data = json_decode($response->getbody()->getcontents(), true);     $data = array_change_key_case($data, case_lower);     $model = satellite::create($data);     dd($data); } 

it looks json key names match nicely model attributes, exception of being capitalised.

try mapping data keys lowercase , creating model instance. per @omisakinoluwatobi suggestion, can use pass true json_decode retrieve data array.

edit - missed response data array of objects. following update iterate on response data , create new satellite each.

// retrieve data response $data = json_decode($response->getbody()->getcontents(), true);  // iterate on response data foreach ($data $attributes) {     // change attribute keys lowercase     $attributes = array_change_key_case($attributes, case_lower);      // create satellite model     satellite::create($attributes); } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -