Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel API: Timestamps (updated_at, created_at) are null

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading