Create Event Calender In Laravel – Info PHP

  • I suppose the Laravel Project is installed on your system.If not then run the below command on your computer and get a fresh laravel 5.5 application structure.

    composer -project –prefer-dist laravel/laravel blog

  • Now you need to install the fullcalender package.To install it fire the below command on your command line(cmd).

    composer require maddhatter/laravel-fullcalendar

  • Once you have installed the package, you need to add it to the service providers and alias.Go to config/app.php and the providers and aliase.All the new package that are installed has to be added in the providers and aliases as well.
    1. ‘providers’ => [  
    2. MaddHatterLaravelFullcalendarServiceProvider::class,  
    3. ],  
    4. ‘aliases’ => [  
    5. ‘Calendar’ => MaddHatterLaravelFullcalendarFacadesCalendar::class,  
    6. ]  
  • Create Events Table
    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.

    1. use IlluminateSupportFacadesSchema;  
    2. use IlluminateDatabaseSchemaBlueprint;  
    3. use IlluminateDatabaseMigrationsMigration;  
    4. class CreateEventsTable extends Migration  
    5. {  
    6.    public function up()  
    7.    {  
    8.       Schema::create(‘events’function (Blueprint $table) {  
    9.       $table->increments(‘id’);  
    10.       $table->string(‘title’);  
    11.       $table->date(‘start_date’);  
    12.       $table->date(‘end_date’);  
    13.       $table->timestamps();  
    14.    });  
    15. }  

    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

  • Now as soon as you have migrated the table into the database,here comes the vital activity of craeting model for “events” table.So just fire the below command and create a new model.

    php artisan make:model

  • As soon as you will run above command,an event.php file will be created in app/event.php.

    Place the fields in the $fillable variable in array format.

    1. namespace App;  
    2. use IlluminateDatabaseEloquentModel;  
    3. class Event extends Model  
    4. {  
    5.    protected $fillable = [‘title’,‘start_date’,‘end_date’];  
    6. }  
  • Now we will create new seeder for dummy records so that we can fill all the events in the calender

    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.

    1. use IlluminateDatabaseSeeder;  
    2. use AppEvent;  
    3. class AddDummyEvent extends Seeder  
    4. {  
    5.  
    6.  
    7.  
    8.   
    9.    public function run()  
    10.       {  
    11.       $data = [  
    12.       [‘title’=>‘Rom Event’‘start_date’=>‘2017-05-10’‘end_date’=>‘2017-05-15’],  
    13.       [‘title’=>‘Coyala Event’‘start_date’=>‘2017-05-11’‘end_date’=>‘2017-05-16’],  
    14.       [‘title’=>‘Lara Event’‘start_date’=>‘2017-05-16’‘end_date’=>‘2017-05-22’],  
    15.       ];  
    16.    foreach ($data as $key => $value) {  
    17.          Event::create($value);  
    18.       }  
    19.    }  
    20. }  
  • Now you run the above seeder by firing the below command

    php artisan db:seed –class=AddDummyEvent

  • Now to display full calender, we need to add a new route “events” in our route file (i.e, web.php) file.

    Route::get(‘events’, ‘EventController@index’);

    By adding the above line we can create route.

  • Now comes the role of controller where we will be writing a method to show calender as well as the events also.Here I have created a controller named EventController.
    1. namespace AppHttpControllers;  
    2. use IlluminateHttpRequest;  
    3. use Calendar;  
    4. use AppEvent;  
    5. class EventController extends Controller  
    6.    {  
    7.    public function index()  
    8.       {  
    9.          $events = [];  
    10.          $data = Event::all();  
    11.          if($data->count()){  
    12.          foreach ($data as $key => $value) {  
    13.          $events[] = Calendar::event(  
    14.          $value->title,  
    15.          true,  
    16.          new DateTime($value->start_date),  
    17.          new DateTime($value->end_date.‘ +1 day’)  
    18.       );  
    19.    }  
    20. }  
    21. $calendar = Calendar::addEvents($events);   
    22.    return view(‘mycalender’, compact(‘calendar’));  
    23.    }  
    24. }  
  • Now as you have created controller, now let us create a view of calender (name it as mycalender.blade.php)

    Kindly paste the below code in your newly created blade file (i.e view).

    1.   
    2. “en”>  
    3.   
    4.   
    5.     >  
    6.     >  
    7.     >  
    8.     “stylesheet” type=“text/css” href=“https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css”>  
    9.     “stylesheet” href=“https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.2.7/fullcalendar.min.css” />   
    10.   
    11.   
    12.     
      class=“container”>  

    13.         
      class=“panel panel-primary”>  

    14.             
      class=“panel-heading”> MY Calender 

        

    15.             
      class=“panel-body”> {!! $calendar->calendar() !!} {!! $calendar->script() !!} 

        

    16.         

      

  •     
  •   

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

    You might also like
    Leave A Reply

    Your email address will not be published.