In this tutorial you will learn about the Laravel 8 FullCalendar Ajax Tutorial with Example and its application with practical example.
In this Laravel 8 FullCalendar Ajax Tutorial I’ll show you how to display events on the calendar using fullcalendar components in laravel 8 application. In this tutorial you will learn to display events on the calendar using fullcalendar in laravel 8. This tutorial you will also learn to show dynamic event data on calendar using fullcalendar components in laravel 8.
Laravel 8 FullCalendar Ajax Tutorial with Example
In this step by step tutorial I will demonstrate you how to show dynamic event data on calendar using fullcalendar components in laravel 8. Please follow instruction given below:
- Step 1 – Install Laravel App
- Step 2 – Connecting App to Database
- Step 3 – Build Migration & Model
- Step 4 – Add Routes
- Step 5 – Create Controller Using Artisan Command
- Step 6 – Create Blade View
- Step 7 – Run Development Server
- Step 8 – Test This App
Step 1 – Install Laravel App
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2 – Connecting App to Database
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=here your database name here DB_USERNAME=here database username here DB_PASSWORD=here database password here |
Step 3 – Build Migration & Model
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan make:model Event -m |
The above command will create a model name Event and also create a migration file for the Events table.Now go to database/migrations folder and open create_events_table.php file. Then put the following code into create_events_table.php file, as follow:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateEventsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('events', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->dateTime('start'); $table->dateTime('end'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('events'); } } |
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan migrate |
Step 4 – Add Routes
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 5 6 7 |
use App\Http\Controllers\FullCalendarController; Route::get('ckeditor', [FullCalendarController::class, 'index']); Route::post('fullcalendar/create', [FullCalendarController::class, 'create']); Route::post('fullcalendar/update', [FullCalendarController::class, 'update']); Route::post('fullcalendar/delete', [FullCalendarController::class, 'destroy']); |
Step 5 – Create Controller Using Artisan Command
Now, lets create a controller named FullCalendarController using command given below –
1 |
php artisan make:controller FullCalendarController |
After successfully create controller go to app/controllers/FullCalendarController.php and update the below code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<?php namespace App\Http\Controllers; use App\Models\Event; use Illuminate\Http\Request; use Redirect,Response; class FullCalenderController extends Controller { 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']); return Response::json($data); } return view('fullcalendar'); } public function create(Request $request) { $insertArr = [ 'title' => $request->title, '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); } } |
Step 6 – Create Blade view
In this step we will create a blade file. Go to app/resources/views and create one file name fullcalendar.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<!DOCTYPE html> <html> <head> <title>Laravel Fullcalender Add/Update/Delete Event Example Tutorial</title> <meta name="csrf-token" content="{{ csrf_token() }}"> </head> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" /> <script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/fullcalendar@3.9.0/dist/fullcalendar.min.css" /> <script src="https://cdn.jsdelivr.net/npm/moment@2.27.0/moment.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/fullcalendar@3.9.0/dist/fullcalendar.min.js"></script> <body> <div class="container"> <div class="response"></div> <div id='calendar'></div> </div> </body> </html> |
Put the script on fullcalendar.blade.php, after the closing of the body tag
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
<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.allDay === 'true') { event.allDay = true; } else { event.allDay = false; } }, selectable: true, selectHelper: true, select: function (start, end, allDay) { var title = prompt('Event Title:'); 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, type: "POST", success: function (data) { displayMessage("Added Successfully"); } }); calendar.fullCalendar('renderEvent', { title: title, 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({ type: "POST", url: SITEURL + 'fullcalendar/delete', data: "&id=" + event.id, 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> |
Step 7 – Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://localhost:8000/fullcalendar |