You can drag-drop, create, read, update, and delete events on the calendar from the database using the FullCalendar frontend library with Livewire in a Laravel 11 application.
Let’s start by building the Event component using Livewire and implement the create, read, update, and delete operations on the calendar from the database using the FullCalendar frontend library:
Step 1: Create Migration and Model
Run the following command to create migration and model file:
php artisan make:model Event -m
Edit database/migrations/events.php
file and add the following fields in it:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::create('events', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->date('start');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::dropIfExists('events');
}
};
Edit app/models/Event.php
file, and add the following property in it:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Event extends Model
{
use HasFactory;
protected $fillable = [
'title', 'start'
];
}
Run migration command to create tables in database:
php artisan migration
Step 2: Install Livewire
Run the following command to install livewire:
composer require livewire/livewire
Step 3: Create Calendar Component
Run the following command to create the component for CRUD event on Calendar:
php artisan make:livewire calendar
Edit app/Livewire/Calendar.php
file and implement CRUD Event operation logic in it:
<?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');
}
}
Edit resources/views/livewire/calendar.blade.php
and create CRUD event form and page that allow users to create, read, delete and update events on fullCalendar:
<style>
#calendar-container{
width: 100%;
}
#calendar{
padding: 10px;
margin: 10px;
width: 1340px;
height: 610px;
border:2px solid black;
}
</style>
<div>
<div id='calendar-container' wire:ignore>
<div id='calendar'></div>
</div>
</div>
@push('scripts')
<script src='https://cdn.jsdelivr.net/npm/[email protected]/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/[email protected]/main.min.css' rel='stylesheet' />
@endpush
Step 4: Create Event CRUD Route
In routes/web.php
file, create routes for perform event CRUD:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Livewire\Calendar;
use App\Models\Event;
Route::view('/', 'home');
Livewire::component('calendar', Calendar::class);
Step 5: Create View File
Create a view file named home.blade.php
in resources/views
directory, and import livewire CRUD Event component in it:
<html>
<head>
@livewireStyles
</head>
<body>
<livewire:calendar />
@livewireScripts
@stack('scripts')
</body>
</html>
Step 6: Test Application
Run the following command to start laravel application server:
php artisan serve
Enter http://localhost:8000/
url on browser for testing.