Blog Detail

04

Jan
How to Manage events on a Google Calendar with Laravel 8 cover image

arrow_back How to Manage events on a Google Calendar with Laravel 8

Laravel-google-calendar is a package by Spatie that makes working with a Google Calendar a breeze. Once it has been set up you can easily create, delete, update things on Google Calendar.

Installation

You can install the package via composer:

composer require spatie/laravel-google-calendar

You must publish the configuration with this command:

php artisan vendor:publish --provider="Spatie\GoogleCalendar\GoogleCalendarServiceProvider"

This will publish a file called google-calendar.php in your config-directory.

Usage

Getting events

You can fetch all events by simply calling Event::get(); this will return all events of the coming year. An event comes in the form of a Spatie\GoogleCalendar\Event object.

The full signature of the function is:

public static function get(Carbon $startDateTime = null, Carbon $endDateTime = null, array $queryParameters = [], string $calendarId = null): Collection

The parameters you can pass in $queryParameters are listed on the documentation on the list at the Google Calendar API docs.

Accessing the start and end dates of an event

You can use these getters to retrieve start and end dates as Carbon instances:

$events = Event::get();

$events[0]->startDate;
$events[0]->startDateTime;
$events[0]->endDate;
$events[0]->endDateTime;

Creating an event

You can just new up a
Spatie\GoogleCalendar\Event-object

$event = new Event;

$event->name = 'A new event';
$event->startDateTime = Carbon\Carbon::now();
$event->endDateTime = Carbon\Carbon::now()->addHour();

$event->save();

You can also call create statically:

Event::create([
   'name' => 'A new event',
   'startDateTime' => Carbon\Carbon::now(),
   'endDateTime' => Carbon\Carbon::now()->addHour(),
]);

This will create an event with a specific start and end time. If you want to create a full-day event you must use startDate and endDate instead of startDateTime and endDateTime.

$event = new Event;

$event->name = 'A new full day event';
$event->startDate = Carbon\Carbon::now();
$event->endDate = Carbon\Carbon::now()->addDay();

$event->save();

You can create an event based on a simple text string like this:

$event = new Event();

$event->quickSave('Appointment at Somewhere on April 25 10am-10:25am');

// statically
Event::quickCreate('Appointment at Somewhere on April 25 10am-10:25am');

Getting a single event

Google assigns a unique id to every single event. You can get this id by getting events using the get method and getting the id property on a Spatie\GoogleCalendar\Event-object:

// get the id of the first upcoming event in the calendar.
$eventId = Event::get()->first()->id;

// you can also get the id after creating the event, then you can save it to database.
$event = new Event;
$newEvent = $event->save();
echo $newEvent->id; // displey the event id

You can use this id to fetch a single event from Google:

Event::find($eventId);

Updating an event

Easy, just change some properties and call save():

$event = Event::find($eventId);

$event->name = 'My updated title';
$event->save();

Alternatively, you can use the update method:

$event = Event::find($eventId)

$event->update(['name' => 'My updated title']);

Deleting an event

Nothing to it!

$event = Event::find($eventId);

$event->delete();

You can obtain the credentials to communicate with Google Calendar, Here’s the complete documentation of this package on Github.

Published at : 04-01-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project