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->description = 'Event description';
$event->startDateTime = Carbon\Carbon::now();
$event->endDateTime = Carbon\Carbon::now()->addHour();
$event->addAttendee([
'email' => 'john@example.com',
'name' => 'John Doe',
'comment' => 'Lorum ipsum',
]);
$event->addAttendee(['email' => 'anotherEmail@gmail.com']);
$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();
Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
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 with these contents:
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'),
];
The first thing you’ll need to do is get credentials to use Google's API. I’m assuming that you’ve already created a Google account and are signed in. Head over to Google API console and click "Select a project" in the header.
Next up we must specify which APIs the project may consume. From the header, select "Enable APIs and Services".
On the next page, search for "Calendar" and select "Google Calendar API" from the list.
From here, press "Enable" to enable the Google Calendar API for this project.
Now that you've created a project that has access to the Calendar API it's time to download a file with these credentials. Click "Credentials" in the sidebar and then press the "Credentials in APIs & Services" link.
From this page, open the "Create credentials" drop-down and select "Service account key".
On the next screen, you can give the service account a name. You can name it anything you’d like. In the service account id you’ll see an email address. We’ll use this email address later on in this guide. Select "JSON" as the key type and click "Create" to download the JSON file. You will get a warning that the service account does not have a role, you can safely ignore this and create the service account without assigning a role.
If you have delegated domain-wide access to the service account and you want to impersonate a user account, specify the email address of the user account in the config file.
Save the json inside your Laravel project at the location specified in the service_account_credentials_json
key of the config file of this package. Because the json file contains potentially sensitive information, I don't recommend committing it to your git repository.
Now that everything is set up on the API site, we’ll need to configure some things on the Google Calendar site. Head over to Google Calendar and view the settings of the calendar you want to work with via PHP. On the "Share with specific people" tab press the "Add people" button and add the service account id that was displayed when creating credentials on the API site.
Scroll down to the "Integrate calendar" section to see the id of the calendar. You need to specify that id in the config file.
This package supports OAuth2 authentication. This allows you to authenticate with an actual Google account, and to create and manage events with your own Google account.
OAuth2 authentication requires a token file, in addition to the credentials file. The easiest way to generate both of these files is by using the php quickstart tool. Following this guide will generate two files, credentials.json
and token.json
. They must be saved to your project as oauth-credentials.json
and oauth-token.json
, respectively. Check the config file in this package for exact details on where to save these files.
To use OAuth2, you must also set a new environment variable in your .env file:
GOOGLE_CALENDAR_AUTH_PROFILE=oauth
If you are upgrading from an older version of this package, you will need to force a publish of the configuration:
php artisan vendor:publish --provider="Spatie\GoogleCalendar\GoogleCalendarServiceProvider" --force
Finally, for a more seamless experience in your application, instead of using the quickstart tool you can set up a consent screen in the Google API console. This would allow non-technical users of your application to easily generate their own tokens. This is completely optional.
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 list
at the Google Calendar API docs.
You can use these getters to retrieve start and end date as Carbon instances:
$events = Event::get();
$events[0]->startDate;
$events[0]->startDateTime;
$events[0]->endDate;
$events[0]->endDateTime;
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');
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 use this id to fetch a single event from Google:
Event::find($eventId);
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']);
Nothing to it!
$event = Event::find($eventId);
$event->delete();
You can set source urls in your events, which are only visible to the creator of the event (see docs for more on the source property). This function only works when authenticated via OAuth.
$yourEvent->source = [
'title' => 'Test Source Title',
'url' => 'http://testsource.url',
];
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.
The only major difference between v1
and v2
is that under the hood Google API v2 is used instead of v1. Here are the steps required to upgrade:
laravel-google-calendar
to google-calendar
client_secret_json
key to service_account_credentials_json
Please see CHANGELOG for more information about what has changed recently.
composer test
Please see CONTRIBUTING for details.
If you discover any security-related issues, please email freek@spatie.be instead of using the issue tracker.
A big thank you to Sebastiaan Luca for his big help creating v2 of this package.
The MIT License (MIT). Please see License File for more information.
laravel 路由访问提示 报错403解决办法 由于laravel 路由命名规则是复数形式。 如果你的路由名称为 Route::resource(‘book’,‘Admin\BookController’); 的形式,当你访问路由的时候会报403错误 所以你的路由应该改为 Route::resource(‘books’,‘Admin\BookController’); 这样问题就解决了 附lar
单一职责原则 一个类和一个方法应该只有一个责任。 例如: public function getFullNameAttribute() { if (auth()->user() && auth()->user()->hasRole('client') && auth()->user()->isVerified()) { return 'Mr. ' . $this->firs
Laravel & Google Drive Storage Demo project with Laravel 5.4 Look at the commit history to see each of the steps I have taken to set this up. Set up this demo project locally git clone git@github.com:
Laravel 是一套简洁、优雅的PHP Web开发框架(PHP Web Framework)。它可以让你从面条一样杂乱的代码中解脱出来;它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁、富于表达力。 功能特点 1、语法更富有表现力 你知道下面这行代码里 “true” 代表什么意思么? $uri = Uri::create(‘some/uri’, array(), array(), tr
我需要空间/Laravel权限的帮助。当我试图分配它给我错误哎呀,看起来像出了问题。 错误 Connection.php第761行中的QueryExcema:SQLSTATE[23000]:完整性约束冲突:1048列role_id不能为空(SQL:插入到(,)值(9,))
Laravel 作为现在最流行的 PHP 框架,其中的知识较多,所以单独拿出来写一篇。 简述 Laravel 的生命周期 Laravel 采用了单一入口模式,应用的所有请求入口都是 public/index.php 文件。 注册类文件自动加载器 : Laravel通过 composer 进行依赖管理,无需开发者手动导入各种类文件,而由自动加载器自行导入。 创建服务容器:从 bootstrap/ap
简介 Laravel Scout 为 Eloquent 模型 全文搜索提供了简单的,基于驱动的解决方案。通过使用模型观察者,Scout 会自动同步 Eloquent 记录的搜索索引。 目前,Scout 自带一个 Algolia 驱动;不过,编写自定义驱动很简单, 你可以轻松的通过自己的搜索实现来扩展 Scout。 安装 首先,通过 Composer 包管理器来安装 Scout: composer
简介 Laravel 致力于让整个 PHP 开发体验变得愉快, 包括你的本地开发环境。 Vagrant 提供了一种简单,优雅的方式来管理和配置虚拟机。 Laravel Homestead 是一个官方预封装的 Vagrant box,它为你提供了一个完美的开发环境,而无需在本地机器安装 PHP 、Web 服务器和其他服务器软件。不用担心会搞乱你的操作系统!Vagrant boxes 是一次性的。如果