-1

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);
    }
  • Where do you set it in your `$final_array`? – brombeer May 16 '22 at 04:26
  • isn't it automatic? it should not be an input field in the final array that's what i didn't put it there –  May 16 '22 at 04:27
  • 2
    It's automatic when you use Eloquent (using the Model), not when using the Query Builder – brombeer May 16 '22 at 04:29
  • Does this answer your question? [Timestamps (updated\_at, created\_at) are null in Laravel 5](https://stackoverflow.com/questions/36998228/timestamps-updated-at-created-at-are-null-in-laravel-5) – brombeer May 16 '22 at 04:29
  • Tried it and it did work,. thanks man! –  May 16 '22 at 04:59

2 Answers2

0

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).

0

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;
Xupitan
  • 1,601
  • 1
  • 8
  • 12