存储功能不传递字段值

Store function is not passing field value

下面的函数

 public function storeTally()
    {
        

        $tally_meal = request()->validate([
                'date_served' => ['required'],
                'meal_type_id' => ['required', Rule::exists('meal_types', 'id')],
                'tally' => ['required'],
         ]);

        $tally_meal['site_id'] = auth()->user()->site->id;
        $tally_meal['user_id'] = auth()->id();
        $tally_meal['sfa_id'] = auth()->user()->sfa->id;
        
        StudentMeal::create($tally_meal);    

        return Redirect::back()->with('success', 'Meal added.');
    }
}

returns 这个错误:

SQLSTATE[HY000]: General error: 1364 Field 'sfa_id' doesn't have a default value

insert into `student_meals` (`date_served`, `meal_type_id`, `tally`, `site_id`, `user_id`, `updated_at`, `created_at`) values (2022-05-30, 1, 1, 1, 1, 2022-05-31 01:50:07, 2022-05-31 01:50:07)

我试过只传递一个整数并得到同样的错误。我不明白为什么它无法识别 sfa_id 字段。

有什么想法吗?

请检查您的学生餐模型 检查 $fillable 变量中是否存在 sfa_id。 如果它不可用,则添加该字段。