Level 13
here what I try
$roll_no = StudentAcadmic::whereHas('courses',function($q) use($course, $created_at){
$q->where('id',$course)
->where('created_at',$created_at)
->where('user_id',Auth::user()->owner->id);
})->orderBy('roll_no','desc')->select('roll_no')->first();
if ($roll_no) {
$roll_nos=$roll_no->roll_no; //here i fetch laat roll no if exist then print or 0
}else{
$roll_nos = 0;
}
$datetime = Carbon::now();
foreach ($students as $key => $value)
{
$data = [
'section_id' => $r->section [$key],
'roll_no' => $roll_nos += 1, //here I'm adding 1 but when section changed it adding 1 but when section change then it initialize from 1
'course_id' => $value->course_id,
'updated_at' => $datetime,
'created_at' => $datetime
];
$value->studentacadmic()->create($data);
}
now problem that here is course have many section so