Skip to main content

Laravel Livewire Fullcalendar Integration Example

Hi Guys,

Today, I will learn you how to use fullcalendar with livewire example. We will explain step-by-step laravel livewire fullcalendar integration. you can easily make livewire fullcalendar integration in laravel. we will describe laravel livewire fullcalendar integration.

Here, I will give you full example for simply livewire fullcalendar integration in laravel native as bellow.

Step 1 : Install Laravel App

In First step, We need to get fresh laravel version application using bellow command. So Let's open terminal and run bellow command.


composer create-project --prefer-dist laravel/laravel blog
Step 2 : Setup Database Configuration

After successfully install laravel app thenafter configure databse setup. We will open ".env" file and change the database name, username and password in the env file.


DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Enter_Your_Database_Name
DB_USERNAME=Enter_Your_Database_Username
DB_PASSWORD=Enter_Your_Database_Password
Step 3 : Install Livewire

In this step, You will simply install livewire to our laravel application using bellow command:


composer require livewire/livewire
Step 4 : Create items Table and Model

In this step we have to create migration for items table using Laravel php artisan command, so first fire bellow command:


php artisan make:model Event -m

After this command you have to put bellow code in your migration file for create items table.

following path: /database/migrations/2021_04_10_102325_create_events_table.php

<?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 we require to run migration be bellow command:


php artisan migrate

After you have to put bellow code in your model file for create items table.

following path:/app/Models/Event.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Event extends Model
{
use HasFactory;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'title',
'start',
];
}
Step:5 Create Route

In thi step,now, we need to add resource route for livewire fullcalendar integration in application. so open your "routes/web.php" file and add following route.


<?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 : Create Component

Now, You can create livewire component using bellow command, So Let's run bellow command to create calendar form component:


php artisan make:livewire calendar
Now they created fies on both path:

app/Http/Livewire/Calendar.php
resources/views/livewire/calendar.blade.php
Now first file we will update as bellow for Calendar.php file. app/Http/Livewire/Calendar.php

<?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');
}
}
Step 7: Create View

Here, we will create blade file for call fullcalendar route. in this file we will use @livewireStyles, @livewireScripts so let's add it

resources/views/home.blade.php

<html>
<head>
@livewireStyles
</head>
<body>
<livewire:calendar />
@livewireScripts
@stack('scripts')
</body>
</html>

after,I will create calendar componet blade file.

resources/views/livewire/calendar.blade.php

<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/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

Now we are ready to run our example so run bellow command for quick run:


php artisan serve
Now you can open bellow URL on your browser:

http://localhost:8000/
Preview
Add Event Fullcalendar
Drag And Drop Fullcalendar

It will help you..

Comments

Popular posts from this blog

React Native Flexbox Tutorial With Example

Hi Dev, Today, I will learn you how to create flexbox in react native. You can easily create flexbox in react native. First i will import namespace View , after I will make flexbox using View tag in react native. Here, I will give you full example for simply display flexbox using react native as bellow. Step 1 - Create project In the first step Run the following command for create project. expo init flexbox Step 2 - App.js In this step, You will open App.js file and put the code. import React, { Component } from 'react' import { View, StyleSheet } from 'react-native' const Home = (props) => { return ( <View style = {styles.container}> <View style = {styles.redbox} /> <View style = {styles.greenbox} /> <View style = {styles.corolbox} /> <View style = {styles.purplebox} /> </View> ) } export default Home const styles = StyleSheet.create ({ container: { flexDirection:...

Laravel 6 validation required if another field is empty

Hii guys, In this artical, i will give you example of laravel 6 in validation required if another field is empty. We know laravel provide several in-built laravel 6 validation required_without . If you need to add validation rules like required if other field is empty in laravel then you can do it using required_without. I am going to explain you, If you can not enter test (field) value at that time test1 (field) is required. So at that time you can add validation required_without. So, you can use this way: "test1" =>"required_without:test" Example: public function store(Request $request) { $request->validate([ "test" =>"required", "test1" =>"required_without:test" ]); dd("Done!"); } If return validation error otherwise show Done!. It will help you...

Laravel 8 Toastr Notifications using yoeunes/toastr package Tutorial

Laravel 8 Toastr Notifications using yoeunes/toastr package Example Hi Guys Today, I will learn with you how to install and use toastr notifications using yoeunes/toastr package in laravel application.we will use yoeunes/toastr package.we will write step be step tutorial for laravel toastr notifications. Toastr notifications yoeunes/toastr package provides warning,success,error and info notifications.You have to just follow few step for implement toastr notifications in your laravel application. In this example i give you example from scratch. So just follow bellow step. Step 1: Install yoeunes/toastr package We need to install yoeunes/toastr composer package for datatable, so you can install using following command: composer require yoeunes/toastr After that you need to set providers and alias. config/app.php 'providers' => [ ... Yoeunes\Toastr\ToastrServiceProvider::class ... ]; As optional if you want to modify the default configuration, you can publish...