laraveleloquentlaravel-collection

Using Laravel Collection in a Loop Without re-declaring the model in each iteration


I'm trying to use a Laravel collection in a loop. I try to do this to minimalize connecting to database since the webserver and database server are in a different servers.

However, every time the loop is moving to second iteration, the collection always returns null.

I want to the second and next iterations still returning data (it does still have data if querying directly into database or declaring collection in each iteration).

This is my sample code:

$opc = M_Opc::query();
$taglist = M_Tags::query();
     foreach($collection as $row){
          $_opc = $opc->where('opc_name',$row['name'])->first();
          $_tags = $taglist->with('plant')->where('id_opc', $_opc->id)->get();
          foreach($_tags as $_tag){
               $plant_name= strtolower(str_replace(" ","_",$_tag->plant->plant_name));
               if(!isset($row[$plant_name]) ||  $row[$plant_name] == "" || empty($row[$plant_name]) || is_null($row[$nm_plant])){
                    continue;
               }
               $_value = $row[$plant_name];
               $model = T_DataOpc::where('id_tag',$_tag->id)->whereDate('sync_date', $this->report_date)
                              ->update(['msdr_value' => $_value, 'harpros_value' => $_value]);
          }
     }

Whenever I tried to run the loop, in the second iteration $_opc always get null value. Same goes to $_tags;

However if I change it to this:

foreach($collection as $row){
          $_opc = M_Opc::where('opc_name',$row['name'])->first();
          $_tags = M_Tags::with('plant')->where('id_opc', $_opc->id)->get();
          foreach($_tags as $_tag){
               $plant_name= strtolower(str_replace(" ","_",$_tag->plant->plant_name));
               if(!isset($row[$plant_name]) ||  $row[$plant_name] == "" || empty($row[$plant_name]) || is_null($row[$nm_plant])){
                    continue;
               }
               $_value = $row[$plant_name];
               $model = T_DataOpc::where('id_tag',$_tag->id)->whereDate('sync_date', $this->report_date)
                              ->update(['msdr_value' => $_value, 'harpros_value' => $_value]);
          }
     }

there's no problem with second iteration and so on.

Please help


Solution

  • Okay. I found out the solution. Turns out I need to turn M_Opc and M_Tags into collection by using get. Here's my solution:

    $opc = M_Opc::get();
    $taglist = M_Tags::with('plant')->get();
         foreach($collection as $row){
              $_opc = $opc->where('opc_name',$row['name'])->first();
              $_tags = $taglist->where('id_opc', $_opc->id)->get();
              foreach($_tags as $_tag){
                   $plant_name= strtolower(str_replace(" ","_",$_tag->plant->plant_name));
                   if(!isset($row[$plant_name]) ||  $row[$plant_name] == "" || empty($row[$plant_name]) || is_null($row[$nm_plant])){
                        continue;
                   }
                   $_value = $row[$plant_name];
                   $model = T_DataOpc::where('id_tag',$_tag->id)->whereDate('sync_date', $this->report_date)
                                  ->update(['msdr_value' => $_value, 'harpros_value' => $_value]);
              }
         }