第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

從 Php Laravel 中的 Api 響應(yīng)填充數(shù)據(jù)庫(kù)

從 Php Laravel 中的 Api 響應(yīng)填充數(shù)據(jù)庫(kù)

PHP
慕田峪9158850 2023-09-22 16:10:28
拜托,我在嘗試從 Guzzlehttp 訪問(wèn)此 Api 響應(yīng)時(shí)一直頭疼,格式是這里的問(wèn)題,我不明白我需要幫助。大家好!拜托,我在嘗試從 Guzzlehttp 訪問(wèn)此 Api 響應(yīng)時(shí)一直頭疼,格式是這里的問(wèn)題,我不明白我需要幫助。這是代碼namespace App\Http\Controllers;use Illuminate\Http\Request;use GuzzleHttp\Client;use App\Country;use App\Globas;class DataController extends Controller{public function getClient(){ $client=new Client();  $response = $client->request('GET', 'https://api.covid19api.com/summary');  $data= json_decode($response->getBody()->getContents(),true);//delete all row in the database tableGlobas::truncate();Country::truncate();  foreach($data as $da){   Globas::create($da);   Country::create([       'Country'=> $data['Country'],       'CountryCode'=>$data['CountryCode'],       'Slug'=>$data['Slug'],       'Date'=>$data['Date'],       'NewConfirmed'=>$data['NewConfirmed'],        'TotalConfirmed'=>$data['TotalConfirmed'],           'NewRecovered'=>$data['NewRecovered'],              'TotalRecovered'=>$data['TotalRecovered'],               'NewDeaths'=>$data['NewDeaths'],               'TotalDeaths'=>$data['TotalDeaths']   ]);return $data;  }}}**This is the Response** {"Global":{"NewConfirmed":259470,"TotalConfirmed":19636772,"NewDeaths":5457,"TotalDeaths":726765,"NewRecovered":201182,"TotalRecovered":11938458},"Countries":[{"Country":"Afghanistan","CountryCode":"AF","Slug":"afghanistan","NewConfirmed":39,"TotalConfirmed":37054,"NewDeaths":5,"TotalDeaths":1312,"NewRecovered":57,"TotalRecovered":25960,"Date":"2020-08-09T17:38:36Z","Premium":[]},......{"Country":"Zimbabwe","CountryCode":"ZW","Slug":"zimbabwe","NewConfirmed":124,"TotalConfirmed":4575,"NewDeaths":0,"TotalDeaths":102,"NewRecovered":71,"Total``Recovered":1416,"Date":"2020-08-09T17:38:36Z","Premium":[]}],"Date":"2020-08-09T17:38:36Z"}**Country Model**<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Country extends Model{   protected $fillable=['Country','CountryCode','Slug','Date',   'NewConfirmed', 'TotalConfirmed','NewRecovered','TotalRecovered','NewDeaths','TotalDeaths'];}謝謝...
查看完整描述

3 回答

?
慕工程0101907

TA貢獻(xiàn)1887條經(jīng)驗(yàn) 獲得超5個(gè)贊

我收到 ErrorException (E_NOTICE) 未定義索引:NewConfirmed


當(dāng)我做了這樣的事情時(shí)


 foreach($data['Countries'] as $country){



     $counter=   Country::create([

               'Country'=> $country['Country'],

               'CountryCode'=>$country['CountryCode'],

               'Slug'=>$country['Slug'],

               'Date'=>$country['Date'],

               'NewConfirmed'=>$country['NewConfirmed'],

                'TotalConfirmed'=>$country['TotalConfirmed'],

                   'NewRecovered'=>$country['NewRecovered'],

                      'TotalRecovered'=>$country['TotalRecovered'],

                       'NewDeaths'=>$country['NewDeaths'],

                       'TotalDeaths'=>$country['TotalDeaths']

           ]);


            }



   foreach($data as $da){

    $globals=Globas::create([

        'NewConfirmed'=>$da['NewConfirmed'],

        'TotalConfirmed'=>$da['TotalConfirmed'],

                       'NewRecovered'=>$da['NewRecovered'],

                          'TotalRecovered'=>$da['TotalRecovered'],

                           'NewDeaths'=>$da['NewDeaths'],

                           'TotalDeaths'=>$da['TotalDeaths']

    ]);

    

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-09-22
?
月關(guān)寶盒

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超5個(gè)贊

在 foreach 內(nèi)部,您應(yīng)該引用 $da 變量,而不是該位置的 $data:


Country::create([

       'Country'=> $data['Country'],

       'CountryCode'=>$data['CountryCode'],

       'Slug'=>$data['Slug'],

       'Date'=>$data['Date'],

       'NewConfirmed'=>$data['NewConfirmed'],

        'TotalConfirmed'=>$data['TotalConfirmed'],

           'NewRecovered'=>$data['NewRecovered'],

              'TotalRecovered'=>$data['TotalRecovered'],

               'NewDeaths'=>$data['NewDeaths'],

               'TotalDeaths'=>$data['TotalDeaths']

   ]);

另一點(diǎn)是國(guó)家也是一個(gè)數(shù)組,因此您可能應(yīng)該為國(guó)家創(chuàng)建另一個(gè) foreach。

https://img1.sycdn.imooc.com//650d4c3600016f2305420668.jpg

foreach ($data['Countries'] as $country)
{

}


查看完整回答
反對(duì) 回復(fù) 2023-09-22
?
慕姐4208626

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超7個(gè)贊

您必須為Global::create()and的返回值分配一個(gè)變量Country::create()。像這樣$global = Global::create(...)運(yùn)行$global->save()將其存儲(chǔ)在數(shù)據(jù)庫(kù)中。


編輯:根據(jù)您最新的回復(fù),您不應(yīng)該迭代數(shù)據(jù),因?yàn)榉祷氐臄?shù)據(jù)不是數(shù)組。對(duì)于全局變量,您不需要迭代。你可以這樣做:


$response = $client->request('GET', 'https://api.covid19api.com/summary');

$data = json_decode($response->getBody()->getContents(),true);


$globals=Globas::create([

    'NewConfirmed'=>$data['NewConfirmed'],

    'TotalConfirmed'=>$data['TotalConfirmed'],

    'NewRecovered'=>$data['NewRecovered'],

    'TotalRecovered'=>$data['TotalRecovered'],

    'NewDeaths'=>$data['NewDeaths'],

    'TotalDeaths'=>$data['TotalDeaths']

]);


$globals->save();


foreach($data['Countries'] as $country) {

     $country = Country::create([

               'Country'=> $country['Country'],

               'CountryCode'=>$country['CountryCode'],

               'Slug'=>$country['Slug'],

               'Date'=>$country['Date'],

               'NewConfirmed'=>$country['NewConfirmed'],

               'TotalConfirmed'=>$country['TotalConfirmed'],

               'NewRecovered'=>$country['NewRecovered'],

               'TotalRecovered'=>$country['TotalRecovered'],

               'NewDeaths'=>$country['NewDeaths'],

               'TotalDeaths'=>$country['TotalDeaths']

           ]);


    $country->save();

}


查看完整回答
反對(duì) 回復(fù) 2023-09-22
  • 3 回答
  • 0 關(guān)注
  • 205 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)