laravel - Array in PHP foreach -




basically trying make array work foreach of mine. array contains data each device (about 10000 devices), of them empty due data being old. need use array in foreach below make sure $newfault gets created if has wrong software. super stuck , cant work.

any clues?

note array has able use relationships of devicelogs here below.

     public function createviacameras() {     set_time_limit(120);       //this part removes entire existing firmwarefault database new info go in.     firmwarefault::truncate();     cameraunlinked::truncate();      //this part imports cameras today.     $today = (new \datetime);     $today->modify('-3 day');     $tomorrow = (new \datetime);     $tomorrow->modify('+1 day');      $devices = device::take(10)->get();         foreach ($devices $device) {       $logs[$device->id] = devicelog::wherenotin('model', ['test', 'test2'])->wheredeviceid($device->id)->wherebetween('created_at', [$today, $tomorrow])->orderby('created_at', 'desc')->first();  }       foreach ($logs $log) {          if (count($log->camera)) {             if ($log->model = $log->camera->name) {                 if ($log->acaps == '1' && $log->version !== $log->camera->fwts) {                         $newfwfault = new firmwarefault();                         $newfwfault->serial = $log->device->serial;                         $newfwfault->fw = $log->version;                         $newfwfault->model = $log->model;                         $newfwfault->new_fw = $log->camera->fwts;                         $newfwfault->fault_id = $log->acaps;                         $newfwfault->save();                     } elseif ($log->acaps == '2' && $log->version !== $log->camera->fwtb) {                         $newfwfault = new firmwarefault();                         $newfwfault->serial = $log->device->serial;                         $newfwfault->fw = $log->version;                         $newfwfault->model = $log->model;                         $newfwfault->new_fw = $log->camera->fwdt;                         $newfwfault->fault_id = $log->acaps;                         $newfwfault->save();                     } elseif ($log->acaps == '3' && $log->version !== $log->camera->fwppt) {                         $newfwfault = new firmwarefault();                         $newfwfault->serial = $log->device->serial;                         $newfwfault->fw = $log->version;                         $newfwfault->model = $log->model;                         $newfwfault->new_fw = $log->camera->fwppt;                         $newfwfault->fault_id = $log->acaps;                         $newfwfault->save();                     } elseif ($log->acaps == '4' && $log->version !== $log->camera->fw) {                         $newfwfault = new firmwarefault();                         $newfwfault->serial = $log->device->serial;                         $newfwfault->fw = $log->version;                         $newfwfault->model = $log->model;                         $newfwfault->new_fw = $log->camera->fw;                         $newfwfault->fault_id = $log->acaps;                         $newfwfault->save();                     } elseif ($log->acaps == '0' && $log->version !== $log->camera->fw) {                         $newfwfault = new firmwarefault();                         $newfwfault->serial = $log->device->serial;                         $newfwfault->fw = $log->version;                         $newfwfault->model = $log->model;                         $newfwfault->new_fw = $log->camera->fw;                         $newfwfault->fault_id = $log->acaps;                         $newfwfault->save();                     } else {                         $newunlink = new cameraunlinked();                         $newunlink->serial = $log->device->serial;                         $newunlink->model = $log->model;                         $newunlink->save();                     }             } else {             $newunlink = new cameraunlinked();             $newunlink->serial = $log->device->serial;             $newunlink->model = $log->model;             $newunlink->save();               }         } else {             $newunlink = new cameraunlinked();             $newunlink->serial = $log->device->serial;             $newunlink->model = $log->model;             $newunlink->save();           };      };      return redirect()->back(); } 

// tried refactoring, hard understand want achieve, or problem is. cleaned code bit , added comments, hope identify goes wrong. refactored loop heavily, (without having way test it) believe actual outcome same before.

public function createviacameras()     {         set_time_limit(120);          //this part removes entire existing firmwarefault database new info go in.         firmwarefault::truncate();         cameraunlinked::truncate();          //this part imports cameras today.         $threedaysago = (new \datetime)->modify('-3 day');         $tomorrow     = (new \datetime)->modify('+1 day');          $devices = device::take(10)->get();  // why 10? might want use limit , offset instead, closer actual query language         $logs = [];                         // instantiate first. if following loop doesn't find logs, $logs loop fail.         foreach ($devices $device) {             $logs[$device->id] = devicelog::wherenotin('model', ['test', 'test2'])                 ->wheredeviceid($device->id)                 ->wherebetween('created_at', [$threedaysago, $tomorrow]) // log created tomorrow???                 ->orderby('created_at', 'desc')                 ->first();                                               // why first log?         }          foreach ($logs $log) {              if (                 !count($log->camera) ||             // $log->camera object. use function check want. checking for`?                 $log->model !== $log->camera->name             ) {                 $model = new cameraunlinked();                 continue;             }              $condition1 = $log->acaps === '1' && $log->version !== $log->camera->fwts;             $condition2 = $log->acaps === '2' && $log->version !== $log->camera->fwtb;             $condition3 = $log->acaps === '3' && $log->version !== $log->camera->fwppt;             $condition4 = $log->acaps === '4' && $log->version !== $log->camera->fw;             $condition5 = $log->acaps === '0' && $log->version !== $log->camera->fw;              if ($condition1 || $condition2 || $condition3 || $condition4 || $condition5) {                 $model = new firmwarefault();                 $model->fw = $log->version;                 $model->fault_id = $log->acaps;                  if ($condition1) {                     $model->new_fw = $log->camera->fwts;                 } elseif ($condition2) {                     $model->new_fw = $log->camera->fwdt;                 } elseif ($condition3) {                     $model->new_fw = $log->camera->fwppt;                 } elseif ($condition4) {                     $model->new_fw = $log->camera->fw;                 } elseif ($condition5) {                     $model->new_fw = $log->camera->fw;                 }             } else {                 $model = new cameraunlinked();             }              $model->serial = $log->device->serial;             $model->model = $log->model;             $model->save();         };          return redirect()->back();     } 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -

python - Read npy file directly from S3 StreamingBody -