0

I am posting data via insomnia to lumen api and want data to be sent to the database but i am experiencing invalid datetime error

I have tried converting the datetime to mysql format but i have failed, both using Carbon, Casting and strtotime php function like below 1. strtotime($value); 2. Carbon::createFromFormat('Y-m-d H:i:s', $value); 3. Casting the date like this 'parking_end_time' => 'datetime:Y-m-d H:i:s',

$router->group(['prefix' => 'api', 'namespace' => 'API'], function 
  () use ($router) {
    /* Check Ins */
    $router->post('checkin', 'CheckInController@store');

   /* Payment Status */
   $router->get('payments/status', 'PaymentStatusController@index');

   /* Vehicle Categories */
    $router->get('vehicles/categories', 
   'VehicleCategoryController@index');

    /* Parking Areas */
    $router->get('parking/areas', 'ParkingAreaController@index');

    /* Parking Types*/
    $router->get('parking/types', 'ParkingTypeController@index');

    /* Vehicles */
    $router->get('vehicles', 'VehicleController@index');
    $router->get('vehicles/{vehicleRegistrationNumber}', 
    'VehicleController@show');
});
// Above are the routes
// Mutator on my model
public function setParkingStartTimeAttribute($value)
{
    $this->attributes['parking_start_time'] = 
    Carbon::createFromFormat($value);
}
// Action in Controller
public function store(Request $request)
{
    $this->validateCheckInRequest($request);
    $parkingStartTime = Carbon::createFromFormat('Y-m-d H:i:s', 
    $request->parking_start_time);

    return response()->json($parkingStartTime); // for testing purposes when i return this it gives me the result below
    $checkIn = CheckIn::create($request->all());
    return new CheckInCollection($checkIn);
}

// Result given when i return the parking_start_time
{
  "date": "-0001-11-30 21:27:00.000000",
  "timezone_type": 3,
  "timezone": "UTC"
}

I expected the datetime return to be like this "0000-00-00 21:27:00" but i see this "-0001-11-30 21:27:00.000000"

Matovu Ronald
  • 782
  • 11
  • 13

2 Answers2

0

You are just returning the carbon instance in below code :

$parkingStartTime = Carbon::createFromFormat('Y-m-d H:i:s', $request->parking_start_time);

You need to apply toDateTimeString() function if you want to format it :

$parkingStartTime = Carbon::createFromFormat('Y-m-d H:i:s', $request->parking_start_time)->toDateTimeString();

Also, in your mutator setParkingStartTimeAttribute function, you are just calling createFromFormat and you are missing the format as first argument.

You need to update :

public function setParkingStartTimeAttribute($value)
{
  return Carbon::createFromFormat('Y-m-d H:i:s', $value);
}

And as your mutator is already doing the formatting work, you can eliminate formatting it again in controller.

Mihir Bhende
  • 8,677
  • 1
  • 30
  • 37
0

Actually i realized what the error was, the error is not in the code, it is about mysql not allowing input of zero dates in the database, and laravel and lumen enforce this rule, that is why i was getting that error, but once i changed the date from zeros it worked just fine.

Some of the references are on these urls

https://www.percona.com/blog/2016/10/18/upgrading-to-mysql-5-7-beware-of-the-new-strict-mode/

Error in mysql when setting default value for DATE or DATETIME

Matovu Ronald
  • 782
  • 11
  • 13