skip to Main Content

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


  1.  Challen::insert($collection->toArray());
    
    Login or Signup to reply.
  2. 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'
        ]);
    }
    
    Login or Signup to reply.
  3. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search