spatie/laravel-google-calendar

repository·main·Indexed 23 days ago

https://github.com/spatie/laravel-google-calendar

A Laravel package for managing Google Calendar events. It supports both Service Account and OAuth2 authentication and provides a wrapper for creating, updating, deleting, and fetching events. Key features include natural language parsing via quickSave/quickCreate, support for full-day events, Google Meet link generation, and a facade for easy integration within Laravel applications.

Tokens
2.9K
Snippets
5
Records
29
Agent score
78%

What's inside spatie/laravel-google-calendar

  1. Set up Service Account authentication

    main

    To use a Service Account:

    1. Create a project in the Google API console.
    2. Enable the Google Calendar API.
    3. Create a Service account key (JSON format) under the Credentials tab.
    4. Save the JSON file to your project (e.g., storage/app/google-calendar/service-account-credentials.json).
    5. Crucial: Go to your Google Calendar settings, find the "Share with specific people" section, and add the Service account email address (found in your JSON file) with permission to manage the calendar.
    6. Copy the Calendar ID from the "Integrate calendar" section of your Google Calendar settings into your config.
  2. Upgrade from v1 to v2

    main

    When upgrading from version 1 to version 2, the underlying Google API changes from v1 to v2. You must perform the following configuration changes:

    1. Rename the configuration file from laravel-google-calendar.php to google-calendar.php.
    2. Inside the configuration file, rename the client_secret_json key to service_account_credentials_json.
  3. Install spatie/laravel-google-calendar

    main

    Install the package via composer and publish the configuration file to set up the service provider.

    composer require spatie/laravel-google-calendar
    
    php artisan vendor:publish --provider="Spatie\GoogleCalendar\GoogleCalendarServiceProvider"
  4. Configure Google Calendar authentication profiles

    main

    The package uses a configuration file config/google-calendar.php to manage authentication. You can choose between service_account (default) or oauth via the default_auth_profile key.

    Key configuration options:

    • default_auth_profile: Set to service_account or oauth (use GOOGLE_CALENDAR_AUTH_PROFILE env var).
    • auth_profiles.service_account.credentials_json: Path to your service account JSON file.
    • auth_profiles.oauth.credentials_json: Path to your OAuth2 credentials JSON file.
    • auth_profiles.oauth.token_json: Path to your OAuth2 token JSON file.
    • calendar_id: The ID of the Google Calendar to use (use GOOGLE_CALENDAR_ID env var).
    return [
        'default_auth_profile' => env('GOOGLE_CALENDAR_AUTH_PROFILE', 'service_account'),
    
        'auth_profiles' => [
            'service_account' => [
                'credentials_json' => storage_path('app/google-calendar/service-account-credentials.json'),
            ],
            'oauth' => [
                'credentials_json' => storage_path('app/google-calendar/oauth-credentials.json'),
                'token_json' => storage_path('app/google-calendar/oauth-token.json'),
            ],
        ],
    
        'calendar_id' => env('GOOGLE_CALENDAR_ID'),
    ];
  5. Event property mapping and accessors

    main

    The Event class uses magic methods to map friendly names to Google Calendar API fields.

    Commonly used property aliases:

    • name maps to summary
    • startDate maps to start.date (for all-day events)
    • endDate maps to end.date (for all-day events)
    • startDateTime maps to start.dateTime (for timed events)
    • endDateTime maps to end.dateTime (for timed events)

    Date Handling:

    • When accessing start.date, end.date, start.dateTime, or end.dateTime, the class automatically returns a Carbon instance.
    • When setting these properties, you should pass a CarbonInterface object.
  6. Create an event

    main

    You can create events using the save() method on a new instance, the static create() method, or by parsing a natural language string.

    Using instance and save():

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

    Using static create():

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

    Full-day events: 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();

    Natural language parsing: Use quickSave() or quickCreate() to parse strings like 'Appointment at Somewhere on April 25 10am-10:25am'.

    $event = new Event();
    $event->quickSave('Appointment at Somewhere on April 25 10am-10:25am');
    
    // Or statically
    Event::quickCreate('Appointment at Somewhere on April 25 10am-10:25am');
  7. Find, update, and delete an event

    main

    To manipulate a specific event, you must first retrieve it using its unique Google ID via Event::find($eventId).

    Find and Update:

    $event = Event::find($eventId);
    
    // Option 1: Property assignment
    $event->name = 'My updated title';
    $event->save();
    
    // Option 2: update() method
    $event->update(['name' => 'My updated title']);

    Delete:

    $event = Event::find($eventId);
    $event->delete();
  8. Fetch events with Event::get()

    main

    Retrieve a collection of events from the configured calendar. By default, it returns events for the coming year.

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

    Note: $queryParameters can include any valid parameters from the Google Calendar API list events documentation.

    $events = Event::get();