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
)
)
3
Answers
You can try this:
i think there is a problem in your array structure it should be like this
[{challen_student_id => 1 ,challen_month => '',challen_due_date => '' ,challen_fine => '' challen_exam_fee =>'' challen_status => 'pending'}]
And always use NUllable fields when dealing with database it saves you many times
laravel provides nullable() function in migration