我嘗試在 fullcalendar 上添加一些事件,歸檔:標題、開始和結束。標題已正確保存,但開始日期和結束日期在我的資料庫中保存錯誤。誰能幫我解決這個問題?
在輸入表中,我選擇了 2022-01-27 - 2022-01-28
在 db 上保存 1970-01-01 - 1970-01-01 并且時間是 00:00
var calendar = $('#calendar').fullCalendar({
editable:true,
header:{
left:'prev,next today',
center:'title',
right:'month,agendaWeek,agendaDay'
},
events:'/event-calender',
selectable:true,
selectHelper: true,
//When u select some space in the calendar do the following:
select: function (start, end, allDay) {
//do something when space selected
//Show 'add event' modal
$('#eventModal').modal('show');
},
$('#eventSubmit').on('click', function(e){
// We don't want this to act as a link so cancel the link action
e.preventDefault();
doSubmit();
});
$('#eventClose').on('click', function(e) {
$('#eventModal').modal('hide');
});
function doSubmit(){
$("#eventModal").modal('hide');
var start = $.fullCalendar.formatDate(start, 'Y-MM-DD HH:mm:ss');
var end = $.fullCalendar.formatDate(end, 'Y-MM-DD HH:mm:ss');
$("#calendar").fullCalendar('renderEvent',
$.ajax ({
url:"/event-calender/action",
type:"POST",
data:{
title: $('#title').val(),
start: start,
end: end,
type: 'add',
}
}),
true);
}
這是控制器
public function action(Request $request)
{
if($request->ajax())
{
if($request->type == 'add')
{
$event = Events::create([
'title' => $request->title,
'start' => $request->start,
'end' => $request->end
]);
// dd($event);
return response()->json($event);
}
}
}
事件模態
class Events extends Model
{
protected $table = 'events';
protected $fillable = [
'title',
'start',
'end'
];
}
我改變了 doSubmit() ,現在我得到了另一個像這樣的錯誤
Uncaught TypeError: Cannot read properties of undefined (reading 'isValid')
uj5u.com熱心網友回復:
您可以嘗試使用碳。
use Carbon\Carbon;
...
$event = Events::create([
'title' => $request->title,
'start => Carbon::parse($request->start),
'end' => Carbon::parse($request->end),
]);
When start and / or end are allowed to be null, you can use Carbon::make() instead of Carbon::parse().
Another option is to use Carbon::createFromFormat('Y-m-d', $request->start);
Also, make sure start and end are added to the $fillable property on the Events model.
uj5u.com熱心網友回復:
ANSWER
//When u select some space in the calendar do the following:
select: function (start, end, allDay) {
//do something when space selected
//Show 'add event' modal
$('#eventModal').modal('show');
var start = $.fullCalendar.formatDate(start, 'Y-MM-DD HH:mm:ss');
var end = $.fullCalendar.formatDate(end, 'Y-MM-DD HH:mm:ss');
$('#eventSubmit').on('click', function(e){
// We don't want this to act as a link so cancel the link action
e.preventDefault();
doSubmit();
});
$('#eventClose').on('click', function(e) {
$('#eventModal').modal('hide');
});
function doSubmit(){
$("#eventModal").modal('hide');
$("#calendar").fullCalendar('renderEvent',
$.ajax ({
url:"/event-calender/action",
type:"POST",
data:{
title: $('#title').val(),
start: start,
end: end,
type: 'add',
},
success:function(data)
{
calendar.fullCalendar('refetchEvents');
// Swal.fire("Event Created Successfully");
}
}),
true);
}
},
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/422577.html
標籤:
