amk
4 months ago

saveMany in loop

Posted 4 months ago by amk

I trying to use this method(saveMany) for each user but it only working on last row of user. Why? It does not working all of user.

     $users = User::all();

      foreach ($bs as $value) {
        $batch_subject[] = new UgStudentSubject([
          'batch_subject_id' => $value->id
        ]);
      }

      foreach ($users as $row) {
        $ug = new UgStudentBatch();
        $ug->ug_batch_id = $request->batch_id;
        $ug->student_code = $row->student_code;
        $ug->save();
        $ug->std_subject()->saveMany($batch_subject);
      }

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