PHP code example of spatie / laravel-google-calendar
1. Go to this page and download the library: Download spatie/laravel-google-calendar library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
spatie / laravel-google-calendar example snippets
use Spatie\GoogleCalendar\Event;
// create a new event
$event = new Event;
$event->name = 'A new event';
$event->description = 'Event description';
$event->startDateTime = Carbon\Carbon::now();
$event->endDateTime = Carbon\Carbon::now()->addHour();
$event->addAttendee([
'email' => '[email protected]',
'name' => 'John Doe',
'comment' => 'Lorum ipsum',
'responseStatus' => 'needsAction',
]);
$event->addAttendee(['email' => '[email protected]']);
$event->addMeetLink(); // optionally add a google meet link to the event
$event->save();
// get all future events on a calendar
$events = Event::get();
// update existing event
$firstEvent = $events->first();
$firstEvent->name = 'updated name';
$firstEvent->save();
$firstEvent->update(['name' => 'updated again']);
// create a new event
Event::create([
'name' => 'A new event',
'startDateTime' => Carbon\Carbon::now(),
'endDateTime' => Carbon\Carbon::now()->addHour(),
]);
// delete an event
$event->delete();
return [
'default_auth_profile' => env('GOOGLE_CALENDAR_AUTH_PROFILE', 'service_account'),
'auth_profiles' => [
/*
* Authenticate using a service account.
*/
'service_account' => [
/*
* Path to the json file containing the credentials.
*/
'credentials_json' => storage_path('app/google-calendar/service-account-credentials.json'),
],
/*
* Authenticate with actual google user account.
*/
'oauth' => [
/*
* Path to the json file containing the oauth2 credentials.
*/
'credentials_json' => storage_path('app/google-calendar/oauth-credentials.json'),
/*
* Path to the json file containing the oauth2 token.
*/
'token_json' => storage_path('app/google-calendar/oauth-token.json'),
],
],
/*
* The id of the Google Calendar that will be used by default.
*/
'calendar_id' => env('GOOGLE_CALENDAR_ID'),
];
GOOGLE_CALENDAR_AUTH_PROFILE=oauth
public static function get(Carbon $startDateTime = null, Carbon $endDateTime = null, array $queryParameters = [], string $calendarId = null): Collection
$event = new Event;
$event->name = 'A new event';
$event->startDateTime = Carbon\Carbon::now();
$event->endDateTime = Carbon\Carbon::now()->addHour();
$event->save();
Event::create([
'name' => 'A new event',
'startDateTime' => Carbon\Carbon::now(),
'endDateTime' => Carbon\Carbon::now()->addHour(),
]);
$event = new Event;
$event->name = 'A new full day event';
$event->startDate = Carbon\Carbon::now();
$event->endDate = Carbon\Carbon::now()->addDay();
$event->save();
$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');
// 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; // display the event id