Create Event Calender In Laravel – Info PHP
composer create-project –prefer-dist laravel/laravel blog
composer require maddhatter/laravel-fullcalendar
- ‘providers’ => [
- MaddHatterLaravelFullcalendarServiceProvider::class,
- ],
- ‘aliases’ => [
- ‘Calendar’ => MaddHatterLaravelFullcalendarFacadesCalendar::class,
- ]
You can create the table manually also.Here I am creating the table by the use o migrations.Create migration of events table using artisan command.So hit the below command on cmd.
php artisan make:migration create_events_table
Firing the above command will create a migration file inside database/migrations, Now open the migration file and paste the below code in it.
- use IlluminateSupportFacadesSchema;
- use IlluminateDatabaseSchemaBlueprint;
- use IlluminateDatabaseMigrationsMigration;
- class CreateEventsTable extends Migration
- {
- public function up()
- {
- Schema::create(‘events’, function (Blueprint $table) {
- $table->increments(‘id’);
- $table->string(‘title’);
- $table->date(‘start_date’);
- $table->date(‘end_date’);
- $table->timestamps();
- });
- }
As soon as you have created the schema in your migration file.Run the below command in cmd in order to migrate the schema into database.
php artisan migrate
php artisan make:model Event
Place the fields in the $fillable variable in array format.
- namespace App;
- use IlluminateDatabaseEloquentModel;
- class Event extends Model
- {
- protected $fillable = [‘title’,‘start_date’,‘end_date’];
- }
php artisan make:seeder AddDummyEvent
After running above command, you will find a seeder file in your seeds folder in the path database/seeds/AddDummyEvent.php, Write the above code in your seeder file created(i.e, in AddDummyEvent.php ) file.
- use IlluminateDatabaseSeeder;
- use AppEvent;
- class AddDummyEvent extends Seeder
- {
- public function run()
- {
- $data = [
- [‘title’=>‘Rom Event’, ‘start_date’=>‘2017-05-10’, ‘end_date’=>‘2017-05-15’],
- [‘title’=>‘Coyala Event’, ‘start_date’=>‘2017-05-11’, ‘end_date’=>‘2017-05-16’],
- [‘title’=>‘Lara Event’, ‘start_date’=>‘2017-05-16’, ‘end_date’=>‘2017-05-22’],
- ];
- foreach ($data as $key => $value) {
- Event::create($value);
- }
- }
- }
php artisan db:seed –class=AddDummyEvent
Route::get(‘events’, ‘EventController@index’);
By adding the above line we can create route.
- namespace AppHttpControllers;
- use IlluminateHttpRequest;
- use Calendar;
- use AppEvent;
- class EventController extends Controller
- {
- public function index()
- {
- $events = [];
- $data = Event::all();
- if($data->count()){
- foreach ($data as $key => $value) {
- $events[] = Calendar::event(
- $value->title,
- true,
- new DateTime($value->start_date),
- new DateTime($value->end_date.‘ +1 day’)
- );
- }
- }
- $calendar = Calendar::addEvents($events);
- return view(‘mycalender’, compact(‘calendar’));
- }
- }
Kindly paste the below code in your newly created blade file (i.e view).
- “en”>
- >
- >
- >
- “stylesheet” type=“text/css” href=“https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css”>
- “stylesheet” href=“https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.2.7/fullcalendar.min.css” />
- class=“container”>class=“panel panel-primary”>class=“panel-heading”> MY Calenderclass=“panel-body”> {!! $calendar->calendar() !!} {!! $calendar->script() !!}
- Above we have created views, controller and route files for displaying calender and its events.Now let us view the result on localhost:8000/events.
Note
Before running the url on the browser,you need to initiate the program by running below command on cmd.php artisan serve
Fig: The Final View of Calender with events showing from database.Article Prepared by Ollala Corp