0

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 3

0
 Challen::insert($collection->toArray());
Sign up to request clarification or add additional context in comments.

Comments

0

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'
    ]);
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.