Laravel API: 时间戳 (updated_at, created_at) 为空

Laravel API: Timestamps (updated_at, created_at) are null

我有一个 API 用户在其中创建某个驱动程序的违规信息,当我尝试使用 Postman 将数据输入 table 时,created_at 列是NULL 不应该的时候。可能是什么问题?

下面的代码是Controller存储数据时的代码行:

public function store(Request $request)
    {
        $rules=[
            'Driver_ID'=>'nullable',
            'Truck_ID'=>'nullable',
            'Date_happened'=>'nullable',
            'Time_happened'=>'nullable',
            'offense_level'=>'nullable',
            'violation_list'=>'required',
            'status'=>'nullable',
            'ml_violation'=>'nullable',

        ];
        $validator = Validator::make($request->all(), $rules);
        if($validator->fails()){
            return response()->json($validator->errors(),400);
        }

       $data = $request->validate([
            'Driver_ID'=>'nullable',
            'Truck_ID'=>'nullable',
            'Date_happened'=>'nullable',
            'Time_happened'=>'nullable',
            'offense_level'=>'nullable',
            'status'=>'nullable',
            'ml_violation'=>'nullable',
       ]);

    //    $violationsInformation = Violations::create($data);


        $violation_list = json_decode($request->input('violation_list'));

        $final_array = [];

        foreach($violation_list as $key => $value){

            array_push($final_array, [
                // 'id'=>$id,
                'Driver_ID'=>  $request->input('Driver_ID'),
                'Truck_ID'=>  $request->input('Truck_ID'),
                'Date_happened'=> $request->input('Date_happened'),
                'Time_happened'=>  $request->input('Time_happened'),
                'violation_list_id'=> $value->id,
                'offense_level'=>  $request->input('offense_level'),
                'status'=>$request->input('status'),
                'ml_violation'=>$request->input('ml_violation'),
            ]);
        }


        //$quizRecords = Quiz_Records::create($final_array);
        // $violationsInformation = Violations::create($data);
        $violationsInformations = DB::table('violation_information')->insert($final_array); // Query Builder approach

       return response(['message'=>"Violation's Information successfully created",
                             'error'=>false,
                             'error_code'=>200,
                             'line'=>"line".__LINE__."".basename(__LINE__),
                             'violationsInformations'=>$violationsInformations],200,[],JSON_NUMERIC_CHECK);
    }

使用 create 方法代替 ->insert 方法。 created_at 和 updated_at 字段由 Eloquent 更新(如果使用 $table->timestamps() 它们默认为 NULL)。

在数据库中

您应该设置 type of created_at : timestampdatetime

Options(ON UPDATE)

defaultCURRENT_TIMESTAMP

sql :

ALTER TABLE `your_table`
CHANGE `created_at` `created_at` datetime NULL DEFAULT CURRENT_TIMESTAMP;