I am trying to show events in DB in fullcalendar ver5 using laravel8. I was ale to show the calendar but each event isn't showing.
I was getting this message in Dev tool so I thought maybe JSON data wasn't being passed correctly but I couldn't figure out.
DB looks like this.
My codes look like this.
web.php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\EventController;
Route::get('/calendar', [EventController::class, 'index'])->name('calendar.index');
namespace App\Http\Controllers;
EventController.php
use Illuminate\Http\Request;
use App\Models\Event;
class EventController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
if ($request->ajax()) {
$events = Event::whereDate('start', '>=', $request->start)
->whereDate('end', '<=', $request->end)
->get();
return response()->json($events);
}
return view('calendar2');
}
calendar2.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset='utf-8' />
<meta name="csrf-token" content="{{ csrf_token() }}">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.2/css/all.css" integrity="sha384-/rXc/GQVaYpyDdyxK+ecHPVYJSN9bmVFBvjA/9eOB+pb3F2w2N6fc5qB9Ew5yIns" crossorigin="anonymous">
<link href="{{ asset('/css/main.css') }}" rel="stylesheet">
<script src="{{ asset('/js/main.js') }}"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
locale: 'en',
selectable: true,
headerToolbar: {
left: 'prevYear,prev,next,nextYear today',
center: 'title',
right: 'addEventButton'
},
customButtons: {
addEventButton: {
text: 'add event',
click: function() {
var dateStr = $('#exampleModal').modal();
var date = new Date(dateStr + 'T00:00:00');
$(function() {
$('#register').click(function(e) {
e.preventDefault();
var vessel_name = $('#vessel-name').val();
var start = $('#start-date').val() + ' ' + $('#start-time').val() + ':00';
var end = $('#end-date').val() + ' ' + $('#end-time').val() + ':00';
$.ajax({
url: "{{ route('calendar.create') }}",
data: {
title: vessel_name,
start: start,
end: end
},
type: 'post',
});
});
});
}
}
},
initialDate: '2022-01-01',
navLinks: true,
editable: true,
dayMaxEvents: true,
events: "{{ route('calendar.index') }}",
});
calendar.render();
});
</script>
<style>
body {
margin: 40px 10px;
padding: 0;
font-family: Arial, Helvetica Neue, Helvetica, sans-serif;
font-size: 14px;
}
#calendar {
max-width: 1100px;
margin: 0 auto;
}
</style>
</head>
<body>
<div id="calendar"></div>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">add event</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group mb-3">
<label for="lastName">title</label>
<input type="text" class="form-control" id="vessel-name" placeholder="">
</div>
<label for="lastName">start</label>
<div class="form-row">
<div class="col-md-6 mb-3">
<input type="date" class="form-control" id="start-date" value="2022-01-01">
</div>
<div class="col-md-6 mb-3">
<input type="time" class="form-control" id="start-time" value="08:00">
</div>
</div>
<label for="lastName">end</label>
<div class="form-row">
<div class="col-md-6 mb-3">
<input type="date" class="form-control" id="end-date" value="2022-01-04">
</div>
<div class="col-md-6 mb-3">
<input type="time" class="form-control" id="end-time" value="08:00">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">close</button>
<button type="button" class="btn btn-primary" id="register">OK</button>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</body>
</html>
Any help would be appreciated as I have tried multiple methods with no success.
Thank you in advance.
You need to restrict the query to the data attributes you need to send.
Therefore, in your controller index
use Illuminate\Http\Request;
use App\Models\Event;
class EventController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
if ($request->ajax()) {
$events = Event::whereDate('start', '>=', $request->start)
->whereDate('end', '<=', $request->end)
->get(['id', 'title', 'start', 'end']);
return response()->json($events);
}
return view('calendar2');
}
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
JQuery is arguably the most popular JavaScript library with so many features for modern development. JQuery is a fast and concise JavaScript library created by John Resig in 2006. It is a cross-platform JavaScript library designed to simplify client-side HTML scripting. Over 19 million websites are currently using jQuery! Companies like WordPress, Facebook, Google, IBM and many more rely on jQuery to provide a kind of web browsing experience.
https://jquery.com/
CSS (Cascading Style Sheets) is a formal language for describing the appearance of a document written using a markup language.
It is mainly used as a means of describing, decorating the appearance of web pages written using HTML and XHTML markup languages, but can also be applied to any XML documents, such as SVG or XUL.
https://www.w3.org/TR/CSS/#css
Bootstrap is not exclusively a CSS framework, but its most popular features are CSS-centric. These include a powerful grid, icons, buttons, map components, navigation bars, and more.
https://getbootstrap.com/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.