Model
class ClickMeeting extends Model
{
protected $table = 'clickmeeting';
public $timestamps = false;
protected $dateFormat = 'U';
protected $guarded = ['id'];
static $videoDemoSource = ['upload', 'youtube', 'vimeo', 'external_link'];
public function ClickMeeting()
{
///
}
}
Controller
public function dashboard()
{
$client = new Client();
$uri = 'https://api.clickmeeting.com/v1/conferences/active';
$header = ['headers' => ['X-Api-Key' => 'xxxxxxxxxxxxxxxxxxxxxxxx']];
$res = $client->get($uri, $header);
$conferences = json_decode($res->getBody()->getContents(), true);
// dd($conferences);
collect($conferences)
->each(function ($conference, $key) {
ClickMeeting::firstOrCreate([
'parent_id' => $conference['parent_id'],
'room_type' => $conference['room_type'],
'room_url' => $conference['room_url'],
],
[
'starts_at' => $conference['starts_at'],
'ends_at' => $conference['ends_at'],
'room_pin' => $conference['room_pin'],
'title' => $conference['name'],
'name_url' => $conference['name_url'],
'access_type' => $conference['access_type'],
'lobby_enabled' => $conference['lobby_enabled'],
'lobby_description' => $conference['lobby_description'],
'registration_enabled' => $conference['registration_enabled'],
'status' => $conference['status'],
'timezone' => $conference['timezone'],
'timezone_offset' => $conference['timezone_offset'],
'paid_enabled' => $conference['paid_enabled'],
'automated_enabled' => $conference['automated_enabled'],
'type' => $conference['type'],
'permanent_room' => $conference['permanent_room'],
'embed_room_url' => $conference['embed_room_url']
]);
});
$conferences = ClickMeeting::get();
return view('admin.clickmeeting.dashboard',compact('conferences'));
SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '2022-06-22T16:10:00 00:00' for column 'starts_at' at row 1 (SQL: insert into
clickmeeting
(parent_id
,room_type
,room_url
,starts_at
,ends_at
,room_pin
,title
,name_url
,access_type
,lobby_enabled
,lobby_description
,registration_enabled
,status
,timezone
,timezone_offset
,paid_enabled
,automated_enabled
,type
,permanent_room
,embed_room_url
) values (?, webinar, https://abc.clickmeeting.com/urinary-tract-infection-in-children, 2022-06-22T16:10:00 00:00, 2022-06-22T17:10:00 00:00, 477736894, URINARY TRACT INFECTION IN CHILDREN, urinary-tract-infection-in-children, 1, 1, , 1, active, Africa/Accra, 0, 0, 0, 0, 0, https://abc.clickwebinar.com/embed_conference.html?r=123456))
I keep getting Invalid datetime format: 1292 Incorrect datetime value. Help is greatly appreciated. Thank you
CodePudding user response:
I think these occur because the DATETIME value in the statement above uses a format that is not supported by MySQL, you can use the STR_TO_DATE() function for passing the starts_at variable into the database.
The code like
'starts_at' => STR_TO_DATE($conference['starts_at'], "%m-%d-%Y %H:%i:%s"),
Please check this link you can find more about the STR_TO_DATE() function
CodePudding user response:
Try use Carbon\Carbon
for assigning your dates:
collect($conferences)
->each(function ($conference, $key) {
ClickMeeting::firstOrCreate([
....
],
[
'starts_at' => Carbon::parse($conference['starts_at']),
'ends_at' => Carbon::parse($conference['ends_at']),
....
]);
});
Alternatively, you could tell laravel which fields are date using the $casts
property:
class ClickMeeting extends Model
{
....
protected $guarded = ['id'];
protected $casts = [
'starts_at' => 'datetime',
'ends_at' => 'datetime'
];
....
}