Home > database >  Laravel Multiple Record with single query not working
Laravel Multiple Record with single query not working

Time:10-05

General error: 1364 Field 'challen_student_id' doesn't have a default value (SQL: insert into challens (updated_at, created_at) values (2022-10-04 08:53:41, 2022-10-04 08:53:41))

$students=Student::all('id');
  $collection = collect($students)->map(function ($student) use($request){
        $collect = collect(['challen_student_id', 
        'challen_month','challen_due_date','challen_fine','challen_exam_fees','challen_status']);
         return $collect->combine([$student->id, $request->item['challen_month'],$request- 
        >item['challen_due_date'],$request->item['challen_fine'],$request- 
        >item['challen_exam_fee'],'pending'])->toArray();
                [enter image description here][1]});

//print_r($collection->toarray());
//exit;
     Challen::create($collection->toArray());

print_r result is

Array ( [0] => Array ( [challen_student_id] => 1 [challen_month] => [challen_due_date] => [challen_fine] => [challen_exam_fee] => [challen_status] => pending )

)

CodePudding user response:

 Challen::insert($collection->toArray());

CodePudding user response:

You can try this:

$students = Students::all();
foreach($students as $key => $value){
    Challen::create([
        'challen_student_id' => $value->id,
        'challen_month'      => $request->item['challen_month'],
        'challen_due_date'   => $request->item['challen_due_date'],
        'challen_fine'       => $request->item['challen_fine'],
        'challen_exam_fees'  => $request->item['challen_exam_fee'],
        'challen_status'     => 'pending'
    ]);
}
  • Related