In this tutorial you will learn about the Laravel 8 maddhatter/laravel-fullcalendar Tutorial with Example and its application with practical example.
In this Laravel 8 maddhatter/laravel-fullcalendar Tutorial with Example I’ll show you how to install and use fullcalendar using maddhatter/laravel-fullcalendar package in laravel. In this tutorial you will learn to display events on the calendar using maddhatter/laravel-fullcalendar package in laravel 8. This tutorial you will also learn to show dynamic event, task or booking data on calendar using fullcalendar components in laravel 8. Fullcalendear package is mainly used appointment booking, event scheduling, task management application development in laravel.
Laravel 8 maddhatter/laravel-fullcalendar Tutorial with Example
In this step by step tutorial I will demonstrate you how to install and integrate 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 – Install maddhatter/laravel-fullcalendar and Build 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 Book -m |
The above command will create a model name Book and also create a migration file for the Events table. Now go to database/migrations folder and open create_books_table.php file. Then put the following code into create_books_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 |
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateBookingsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('bookings', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('title'); $table->date('start'); $table->date('end'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('bookings'); } } ?> |
Now, go to app/models and open Book.php file and add the following code into it:
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php namespace App/Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Booking extends Model { use HasFactory; protected $fillable = ['title','start','end']; } ?> |
Now, run the migration to create database table using following artisan command:
1 |
php artisan migrate |
Step 4 – Install maddhatter/laravel-fullcalendar and Build Routes
In this step, we will install maddhatter/laravel-fullcalendar Package via the composer dependency manager. Use the following command to install maddhatter/laravel-fullcalendar Package.
1 |
composer require maddhatter/laravel-fullcalendar |
Now, open “config/app.php” file and register this package, as shown below:
1 2 3 4 5 6 7 8 |
'providers' => [ .... MaddHatter\LaravelFullcalendar\ServiceProvider::class, ], 'aliases' => [ .... 'Calendar' => MaddHatter\LaravelFullcalendar\Facades\Calendar::class, ] |
Create 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 |
use App\Http\Controllers\FullCalendarController; Route::get('/booking',[FullCalendarController::class, 'index']); |
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, visit app/http/controllers directory and open FullCalendarController.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 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Booking; use Redirect,Response; class FullCalendarController extends Controller { public function index() { $events = []; $data = Booking::all(); if($data->count()){ foreach ($data as $key => $value) { $events[] = Calendar::event( $value->title, true, new \DateTime($value->start), new \DateTime($value->end.' +1 day') ); } } $calendar = Calendar::addEvents($events); return view('fullcalendar', compact('calendar')); } } ?> |
Step 6 – Create Blade view
In this step we will create blade view file. Go to resources/views directory and create a blade view file named calendar.blade.php. 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 |
<!doctype html> <html lang="en"> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.2.7/fullcalendar.min.js"></script> <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.2.7/fullcalendar.min.css"/> </head> <body> <div class="container"> <div class="panel panel-primary"> <div class="panel-heading"> MY Calender </div> <div class="panel-body" > {!! $calendar->calendar() !!} {!! $calendar->script() !!} </div> </div> </div> </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 |
http://localhost:8000/booking |