Javasacript - problema com fullcalendar em laravel
boas, tenho um calendario a funcionar em parte, o que acontece é que se eu criar um evento ele mostra o evento criado e guarda ele na base de dados, mas se eu atualizar a pagina, ele desaparece mas continua na base de dados, e tambem não mostra os eventos criados anteriormente e que estão guardados na base de dados, como posso resolver?
<script> $(document).ready(function () { var SITEURL = "{{url('/')}}"; $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); var calendar = $('#calendar').fullCalendar({ editable: true, events: SITEURL + "/fullcalendar", displayEventTime: true, editable: true, eventRender: function (event, element, view) { if (event.limit >= 5) { element.css('background-color', '#FF0000') } if (event.limit <= 5) { element.css('background-color', '#FFFF00') } if (event.allDay === 'true') { event.allDay = true; } else { event.allDay = false; } }, selectable: true, selectHelper: true, useCurrent: false, select: function (start, end, allDay) { var title = prompt('Evente Title:'); var limit = prompt('Limite de alunos:'); if (title) { var start = $.fullCalendar.formatDate(start, "Y-MM-DD HH:mm:ss"); var end = $.fullCalendar.formatDate(end, "Y-MM-DD HH:mm:ss"); $.ajax({ url: SITEURL + "/fullcalendar/create", data: 'title=' + title + '&start=' + start + '&end=' + end + '&limit=' + limit, type: "POST", success: function (data) { displayMessage("Added Successfully"); } }); calendar.fullCalendar('renderEvent', { title: title, limit: limit, start: start, end: end, allDay: allDay }, true ); } calendar.fullCalendar('unselect'); }, eventDrop: function (event, delta) { var start = $.fullCalendar.formatDate(event.start, "Y-MM-DD HH:mm:ss"); var end = $.fullCalendar.formatDate(event.end, "Y-MM-DD HH:mm:ss"); $.ajax({ url: SITEURL + '/fullcalendar/update', data: 'title=' + event.title + '&start=' + start + '&end=' + end + '&id=' + event.id, type: "POST", success: function (response) { displayMessage("Updated Successfully"); } }); }, eventClick: function (event) { var deleteMsg = confirm("Do you really want to delete?"); if (deleteMsg) { $.ajax({ url: SITEURL + '/fullcalendar/delete', data: "&id=" + event.id, type: "POST", success: function (response) { if(parseInt(response) > 0) { $('#calendar').fullCalendar('removeEvents', event.id); displayMessage("Deleted Successfully"); } } }); } } }); }); function displayMessage(message) { $(".response").html("<div class='success'>"+message+"</div>"); setInterval(function() { $(".success").fadeOut(); }, 1000); } </script>
de seguida fica o codigo do controller que manipula a base de dados public function index() { if(request()->ajax()) { $start = (!empty($_GET["start"])) ? ($_GET["start"]) : (''); $end = (!empty($_GET["end"])) ? ($_GET["end"]) : (''); $data = Event::whereDate('start', '>=', $start)->whereDate('end', '<=', $end)->get(['id','title','start', 'end', 'limit']); return Response::json($data); } return view('fullcalendar'); } public function create(Request $request) { $insertArr = [ 'title' => $request->title, 'limit' => $request->limit, 'start' => $request->start, 'end' => $request->end ]; $event = Event::insert($insertArr); return Response::json($event); } public function update(Request $request) { $where = array('id' => $request->id); $updateArr = ['title' => $request->title,'start' => $request->start, 'end' => $request->end]; $event = Event::where($where)->update($updateArr); return Response::json($event); } public function destroy(Request $request) { $event = Event::where('id', $request->id)->delete(); return Response::json($event); }
Pedro Goncalves
Curtidas 0