-1

i have method to mass update the studentSection table. This method works. But if i mass update multiple records only one record gets save.

Update (latest)

public function setStudentsSection(Request $request)
    {
        $enrollments = Enrollment::whereIn('student_id', $request->students)->where('session_id', $request->session_id)->get();
        $program_section = ProgramSection::withCount('students')->find($request->program_section_id);
        if(($program_section->students_count + count($enrollments)) <= $program_section->max_students) {
            $data = [];
            foreach($enrollments as $enrollment) {
                $data[] = [
                    'student_id'    => $enrollment->student_id,
                    'enrollment_id' => $enrollment->id,
                    'section_id'    => $request->program_section_id,
                    'created_at'    => Carbon::now()
                ];
            }
            StudentSection::createMany($data);
        }
        return response()->json(['errors' => ['message' => 'Selected Section is full.']], 405);
    }

Second approach: This works but it does not trigger my observer that outputs message. Assigned student to section

public function setStudentsSection(Request $request)
    {
        $enrollments = Enrollment::whereIn('student_id', $request->students)->where('session_id', $request->session_id)->get();
        $program_section = ProgramSection::withCount('students')->find($request->program_section_id);
        if(($program_section->students_count + count($enrollments)) <= $program_section->max_students) {
            $new_student_sections = array();
            foreach($enrollments as $enrollment) {
                $data = [
                    'student_id'    => $enrollment->student_id,
                    'enrollment_id' => $enrollment->id,
                    'section_id'    => $request->program_section_id,
                    'created_at'    => Carbon::now()
                ];
                array_push($new_student_sections, $data);
            }
            return StudentSection::insert($new_student_sections);
        }
        return response()->json(['errors' => ['message' => 'Selected Section is full.']], 405);
    }

Here's my relationship

Enrollment Model

public function studentSection()
    {
        return $this->hasOne('App\Models\Student\Section');
    }

StudentSection model

public function enrollment()
    {
        return $this->belongsTo('App\Models\Enrollment', 'enrollment_id');
    }
Kael
  • 161
  • 2
  • 13
  • Yes bro here's my fillable of StudentSection ```protected $fillable = [ 'student_id', 'section_id', 'enrollment_id', ];``` – Kael Sep 12 '22 at 02:21
  • Hello bro thanks for the time in replying in novice question. I have done what you suggested i get this ```"Property [student_id] does not exist on this collection instance."```. Also updated the post. – Kael Sep 12 '22 at 02:41
  • You have wrongly implemented it, Okay wait, I will answer it. – JS TECH Sep 12 '22 at 02:46

1 Answers1

2

Third approach

if(($program_section->students_count + count($enrollments)) <= $program_section->max_students) {

    foreach($enrollments as $enrollment) {

        $enrollment->studentSection()->create([
            'student_id'    => $enrollment->student_id,
            'section_id'    => $request->program_section_id,
            'created_at'    => Carbon::now() //optional
        ]);

    }

    return response('success');

}
return response()->json(['errors' => ['message' => 'Selected Section is full.']], 405);
JS TECH
  • 1,556
  • 2
  • 11
  • 27
  • Yeah bro it was giving 500 status tried fixing so i ended up with my for loop also. update the code bro but this gives me ```Call to undefined method App\\Models\\Student\\Section::createMany()``` – Kael Sep 12 '22 at 03:17
  • Yes bro i have ```hasOne StudentSection in Enrollment model``` and ```belongsTo enrollment in StudentSection Model``` – Kael Sep 12 '22 at 03:24
  • wait bro i'll post my relationship in models – Kael Sep 12 '22 at 03:25
  • Sorry bro it took me a while to reply. I tried the code but it results to 405 section full. I reverted back to my original it saves the records. – Kael Sep 12 '22 at 03:51
  • check also the relationship names it's correct but it give me 405 status. – Kael Sep 12 '22 at 03:58
  • [2022-09-12 04:04:56] local.INFO: {"student_id":75,"enrollment_id":78,"section_id":16,"updated_at":"2022-09-12T04:04:56.000000Z","created_at":"2022-09-12T04:04:56.000000Z","id":84} [2022-09-12 04:04:56] local.INFO: {"student_id":76,"enrollment_id":79,"section_id":16,"updated_at":"2022-09-12T04:04:56.000000Z","created_at":"2022-09-12T04:04:56.000000Z","id":85} Try logging it. This gives me the data – Kael Sep 12 '22 at 04:07
  • Also you have to used `return` after `foreach` loop. – JS TECH Sep 12 '22 at 04:22
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/247974/discussion-between-js-tech-and-kael). – JS TECH Sep 12 '22 at 04:28