Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer. You can expect quick tips, links to interesting tutorials, opinions and packages. Want to learn the cool stuff? Then sign up now!

A package to manage events on a Google Calendar

Like previously mentioned we’re currently building a new dashboard to display on our wall mounted TV at the office. One of the things we want to show on that dashboard are important events for our company. Things like when a site goes live, when there’s a conference we’re going to visit, when we’re doing our monthly visit to our favourite Italian restaurant and so on. We manage these events on a Google Calendar. To make working with such a calendar real easy we’ve made a new package called laravel-google-calendar.

The steps required to install the package should be very familiar: pull it in via Composer, register a service provider / facade and finally publish a config file. After that you’ll need to get some authentication credentials from Google. Normally this takes quite some time as the google docs are very confusing, but if you follow my previous blogpost on the subject you’ll be set up in no time.

With the package fully installed you can do these things:

use Spatie\GoogleCalendar\Event;

//create a new event
$event = new Event;

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

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

// create a new event with the static method
'name' => 'A new event',
'startDateTime' => Carbon\Carbon::now(),
'endDateTime' => Carbon\Carbon::now()->addHour(),

// get all future events on a calendar, returns a Collection
$events = Event::get();
$firstEvent = $events->first();

// find a specific event and update item
$event = Event::find($eventId)
$event->name = 'My updated title'

// delete an event

It’s also possible to work with multiple calendars. The create, get and find methods take a second parameter $calendarId.

The Google Calendar API provides many options. It’s a big beast really. This package doesn’t support all those options. For instance recurring events cannot be managed properly with our code. But if you stick to creating events with a name, a description and a date(time) you should be fine.

We hope you’ll like our package. If you have any questions or remarks on it let me know in the comments below or the issue tracker on GitHub. We’ve made a bunch of other packages in the past. Check out this list on our site to find out if there’s something else we’ve made that you can use on your next project.

Freek Van der Herten is a partner and developer at Spatie, an Antwerp based company that specializes in creating web apps with Laravel. After hours he writes about modern PHP and Laravel on this blog. When not coding he’s probably rehearsing with his kraut rock band.
  • Michiel Zoer

    Hi! Love the Package.

    Can I get the Google Calendar unique event-id after creating it so we can store it in our DB?

    This is how we create an event:

    $googleEvent = new SpatieGoogleCalendarEvent;
    $googleEvent->name = $request->title;
    $googleEvent->startDateTime = Carbon::parse($request->startDateTime);
    $googleEvent->endDateTime = Carbon::parse($request->endDateTime);

    > GoogleEvent unique id = $googleEvent->id? (this doesn’t work)