copain
1 year ago

Foreach loop to array

Posted 1 year ago by copain

with my current code i can only store 1 value at a time is there anyway that i could make this array? i use for-each loop hoping it will loop all the value and send it as array, but not working can anyone point me to a Laravel tutorial discussing on how to perform loop to array..

Note: my model Score has foreign key which is [Lead_id and Subject_id]

Controller code:

 public function store(Request $request)
{
$data = $request->all();

        $leads = $data['Lead_id'];
    
        $subject_ids = $data['Subject_id'];
    
        foreach($leads as $key => $input) {

        $scores = new Score();
        $scores->Subject_id = isset($subject_ids[$key]) ?  $subject_ids[$key] : ''; 
        $scores->Lead_id = isset($leads[$key]) ? $leads[$key] : ''; 
        $scores->save();
      }
}

Please sign in or create an account to participate in this conversation.