วิธีเพิ่มเงื่อนไขที่ไม่เป็นโมฆะให้กับงาน cron ใน Laravel

ใน Laravel-5.8 ของฉัน ฉันมีงาน cron กำลังทำงานอยู่

public function handle()
{
    $client = new Client();
    $res = $client->request('GET','https://api.laravel.com/my-workers', [
       'query' => ['key' => 'kkdfhfn7755222']
   ])->getBody();

    $clientdatas = json_decode($res->getContents(), true);  
    
    foreach($clientdatas as $clientdata)
    {        
     
        $testing = HrTesting::updateOrCreate([
            'testing_name' => $clientdata['testing_location']
        ],
        [
                 'company_id'   => 1,          
        ]); 
    }

   }

ฉันพบว่า $clientdata['testing_location'] เป็นโมฆะ แต่ก็ยังสร้าง / อัปเดตในตาราง

ฉันจะบอกได้อย่างไรว่าเมื่อเป็นโมฆะก็ไม่ควรดำเนินการใดๆ

ขอบคุณ


person mikefolu    schedule 21.07.2020    source แหล่งที่มา
comment
ดังนั้นฉันต้องเพิ่ม if($clientdata['testing_location'] === null) { continue; } ภายใน foreach loop ใช่ไหม? :)   -  person Oleg Nurutdinov    schedule 21.07.2020


คำตอบ (1)


เพียงเพิ่มลงใน foreach loop ของคุณ

    foreach($clientdatas as $clientdata)
    {        
        if (isset($clientdata['testing_location']) && $clientdata['testing_location'] !== null) { 
            $testing = HrTesting::updateOrCreate([
                'testing_name' => $clientdata['testing_location']
            ],[
                 'company_id'   => 1,          
            ]); 
        }
    }

person Kurt Friars    schedule 21.07.2020