I’m working on a Laravel project where I’m sending date values as object of string type from my frontend using AJAX and trying to convert them to Carbon date objects in my controller
package used. – > https://github.com/spatie/laravel-google-calendar
frontend code
var startDate = $('.effective_period_start_date').val();
var endDate = $('.effective_period_end_date').val();
if (startDate && endDate) {
// Prepare the data to send to the server
// var data = {// Include CSRF token
// "effective_period_start_date": startDate,
// "effective_period_end_date": endDate
// };
$.ajax({
type: 'POST',
url: url,
headers: {
'X-CSRF-TOKEN': CSRF_TOKEN
},
data: {
effective_period_start_date: startDate,
effective_period_end_date: endDate
},
CONTROLLER
$startDate = $request->effective_period_start_date;
$endDate = $request->effective_period_end_date;
$startDateObject = Carbon::createFromFormat('Y-m-d', $startDate);
$endDateObject = Carbon::createFromFormat('Y-m-d', $endDate);
$Userdescription = 'Event description';
$event = Event::create([
'startDateTime' => $startDateObject,
'endDateTime' => $endDateObject,
'description' => $Userdescription
]);
$event->save();
I’m getting an “Array to string conversion” error at the line where I try to create Carbon date objects. How can I resolve this issue?