Home > Software design >  Laravel API: Timestamps (updated_at, created_at) are null
Laravel API: Timestamps (updated_at, created_at) are null

Time:05-16

I have an API where the user creates the violation information of a certain driver and when I try to input a data into the table using Postman, the created_at column is NULL when it should not be. What could be the problem?

enter image description here

The code below is the lines of code Controller of when storing the data:

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

CodePudding user response:

In database

You should set type of created_at : timestamp or datetime

And Options is (ON UPDATE)

And default is CURRENT_TIMESTAMP

sql :

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

CodePudding user response:

Instead of ->insert method use create method. The created_at and updated_at fields are updated by Eloquent (If using $table->timestamps() they are default NULL).

  • Related