In this tutorial you will learn about the Laravel Livewire Fullcalendar Integration Example and its application with practical example.
In this Laravel Livewire Fullcalendar Integration Example tutorial I’ll show you how to display events on the calendar using livewire fullcalendar components in laravel 8 application. In this tutorial you will learn to display events on the calendar using livewire fullcalendar in laravel 8. This tutorial you will also learn to show dynamic event data on calendar using livewire fullcalendar components in laravel.
Laravel Livewire Fullcalendar Integration Example
In this step by step tutorial I will demonstrate you how to display events on the calendar using livewire fullcalendar components in laravel 8 applications. Please follow the instruction given below:
- Step 1: Install Laravel 8 App
- Step 2: Add Database Detail
- Step 3: Install Livewire Package
- Step 4: Create FullCalendar Component using Artisan
- Step 5: Add Route For Livewire FullCalendar
- Step 6: Add Code On View File
- Step 7: Run Development Server
Step 1: Install Laravel 8 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: Add Database Detail
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 |
Create Model & Migration
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan make:model Event -m |
Then visit /database/migrations directory and open event.php file and add the following code into it:
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 |
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateEventsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('events', function (Blueprint $table) { $table->id(); $table->string('title'); $table->string('start'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('events'); } } |
Now, run the migration to create database table using following artisan command:
1 |
php artisan migrate |
Step 3: Install Livewire Package
In this step, we will install livewire Package via the composer dependency manager. Use the following command to install livewire Package.
1 |
composer require livewire/livewire |
Step 4: Create FullCalendar Component using Artisan
In this step we will create livewire calendar component using following command:
1 |
php artisan make:livewire calendar |
The above command will create the following components on the following path:
1 2 3 |
app/Http/Livewire/Calendar.php resources/views/livewire/calendar.blade.php |
Now, go to app/Http/Livewire folder and open Calendar.php file. Then add the following code into your Calendar.php file:
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 |
<?php namespace App\Http\Livewire; use Livewire\Component; use App\Models\Event; class Calendar extends Component { public $events = ''; public function getevent() { $events = Event::select('id','title','start')->get(); return json_encode($events); } /** * Write code on Method * * @return response() */ public function addevent($event) { $input['title'] = $event['title']; $input['start'] = $event['start']; Event::create($input); } /** * Write code on Method * * @return response() */ public function eventDrop($event, $oldEvent) { $eventdata = Event::find($event['id']); $eventdata->start = $event['start']; $eventdata->save(); } /** * Write code on Method * * @return response() */ public function render() { $events = Event::select('id','title','start')->get(); $this->events = json_encode($events); return view('livewire.calendar'); } } |
Now, go to resources/views/livewire folder and open calendar.blade.php file. Then put the following code into your calendar.blade.php file:
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 |
<div> <div id='calendar-container' wire:ignore> <div id='calendar'></div> </div> </div> @push('scripts') <script src='https://cdn.jsdelivr.net/npm/fullcalendar@5.3.1/main.min.js'></script> <script> document.addEventListener('livewire:load', function() { var Calendar = FullCalendar.Calendar; var Draggable = FullCalendar.Draggable; var calendarEl = document.getElementById('calendar'); var checkbox = document.getElementById('drop-remove'); var data = @this.events; var calendar = new Calendar(calendarEl, { events: JSON.parse(data), dateClick(info) { var title = prompt('Enter Event Title'); var date = new Date(info.dateStr + 'T00:00:00'); if(title != null && title != ''){ calendar.addEvent({ title: title, start: date, allDay: true }); var eventAdd = {title: title,start: date}; @this.addevent(eventAdd); alert('Great. Now, update your database...'); }else{ alert('Event Title Is Required'); } }, editable: true, selectable: true, displayEventTime: false, droppable: true, // this allows things to be dropped onto the calendar drop: function(info) { // is the "remove after drop" checkbox checked? if (checkbox.checked) { // if so, remove the element from the "Draggable Events" list info.draggedEl.parentNode.removeChild(info.draggedEl); } }, eventDrop: info => @this.eventDrop(info.event, info.oldEvent), loading: function(isLoading) { if (!isLoading) { // Reset custom events this.getEvents().forEach(function(e){ if (e.source === null) { e.remove(); } }); } } }); calendar.render(); @this.on(`refreshCalendar`, () => { calendar.refetchEvents() }); }); </script> <link href='https://cdn.jsdelivr.net/npm/fullcalendar@5.3.1/main.min.css' rel='stylesheet' /> @endpush |
Step 5: Add Route For Livewire Fullcalendar
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 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php use Illuminate\Support\Facades\Route; use App\Http\Livewire\Calendar; use App\Models\Event; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::view('/', 'home'); Livewire::component('calendar', Calendar::class); |
Step 6: Add Code On View File
In this step we will create blade view file. Go to resources/views/ folder and open view files that name home.blade.php file. Then add the following code into your home.blade.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<html> <head> <title>Laravel Livewire Fullcalendar Example</title> @livewireStyles <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script> <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" /> <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script> </head> <body> <livewire:calendar /> @livewireScripts @stack('scripts') </body> </html> |
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 |
localhost:8000/ |