ecasanes/laravel-google-calendar-php-6

Manage events on a Google Calendar (Compatible with PHP 5.6.15 up)

1.1.0 2017-04-26 13:05 UTC

This package is not auto-updated.

Last update: 2024-04-13 23:31:03 UTC


README

Latest Version on Packagist Software License Build Status SensioLabsInsight Quality Score StyleCI Total Downloads

This package makes working with a Google Calendar a breeze. Once it has been set up 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();
$event->addAttendee(['email' => 'youremail@gmail.com']);
$event->addAttendee(['email' => 'anotherEmail@gmail.com']);

$event->save();

// get all future events on a calendar
$events = Event::get();

$firstEvent = $events->first();
$firstEvent->name = 'updated name';
$firstEvent->save();

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

// delete an event
$event->delete();

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

Postcardware

You're free to use this package (it's MIT-licensed), but if it makes it to your production environment you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

The best postcards will get published on the open source page on our website.

Installation

You can install the package via composer:

composer require ecasanes/laravel-google-calendar-php-6

Next up the service provider must be registered:

'providers' => [
    ...
    Spatie\GoogleCalendar\GoogleCalendarServiceProvider::class,
];

Optionally the Spatie\GoogleCalendar\GoogleCalendarFacade must be registered:

'aliases' => [
	...
    'GoogleCalendar' => Spatie\GoogleCalendar\GoogleCalendarFacade::class,
    ...
]

You must publish the configuration with this command:

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

This will publish file called laravel-google-calendar.php in your config-directory with this contents:

<?php

return [

    /**
     * Path to a json file containing the credentials of a Google Service account.
     */
    'client_secret_json' => storage_path('app/laravel-google-calendar/client_secret.json'),

    /**
     *  The id of the Google Calendar that will be used by default.
     */
    'calendar_id' => '',

];

Read this blogpost to learn how to get the correct values for client_secret_json and calendar_id.

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:

/**
 * @param \Carbon\Carbon|null $startDateTime
 * @param \Carbon\Carbon|null $endDateTime
 * @param array $queryParameters
 * @param string|null $calendarId
 *
 * @return \Illuminate\Support\Collection
 */
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 list at the Google Calendar API docs.

Accessing start and end dates of an event

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

$events = Event::get();

$event[0]->startDate;
$event[0]->startDateTime;
$event[0]->endDate;
$event[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();

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.
$calendarId = Event::get()->first()->id;

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

Event::find($calendarId);

Updating an event

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

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

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

Deleting an event

Nothing to it!

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

$event->delete();

Limitations

The Google Calendar API provides many options. This package doesn't support all of them. For instance recurring events cannot be managed properly with this package. If you stick to creating events with a name and a date you should be fine.

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker.

Credits

About Spatie

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

License

The MIT License (MIT). Please see License File for more information.