spatie/calendar-links
Generate “Add to calendar” links and ICS files for events. Supports Google Calendar, iCal/Apple Calendar, Outlook and more. Define title, start/end, description and location, then get shareable URLs or downloadable .ics content for your app.
Installation:
composer require spatie/calendar-links
No additional configuration is required—just import and use.
First Use Case: Generate a Google Calendar link for an event:
use Spatie\CalendarLinks\Link;
$link = Link::create(
'Team Sync',
now()->addHours(2), // Start time
now()->addHours(3) // End time
)->google();
Output: A clickable URL to add the event to Google Calendar.
Where to Look First:
Event Creation:
Use Link::create() with:
DateTime): Required for duration.Link::create('Meeting', $start, $end)
->description('Project review')
->location('Zoom Room')
->attendee('user@example.com');
Calendar Integration: Chain methods to target specific providers:
$link->google(); // Google Calendar
$link->outlook(); // Outlook
$link->apple(); // Apple Calendar
$link->ical(); // iCal (.ics file)
Dynamic Generation: Generate links in Blade templates or controllers:
// Blade
<a href="{{ Link::create('Event', $start, $end)->google() }}">Add to Google</a>
// Controller
return response()->json(['google_link' => $link->google()]);
Batch Processing: Loop through events to generate multiple links:
foreach ($events as $event) {
$links[$event->id] = [
'google' => Link::create($event->title, $event->start, $event->end)->google(),
'ical' => Link::create($event->title, $event->start, $event->end)->ical(),
];
}
Laravel Routes: Create a dedicated route for calendar links:
Route::get('/events/{event}/calendar/{provider}', function ($event, $provider) {
$eventData = Event::findOrFail($event);
$link = Link::create($eventData->title, $eventData->start, $eventData->end);
return redirect()->to($link->$provider());
});
Example URL: /events/123/calendar/google.
API Endpoints: Return links as JSON for frontend frameworks:
Route::get('/events/{event}/calendar-links', function ($event) {
$eventData = Event::findOrFail($event);
return [
'google' => Link::create(...)->google(),
'outlook' => Link::create(...)->outlook(),
];
});
All-Day Events:
Use allDay() for events without a time:
Link::create('Conference', $date, $date)->allDay()->google();
Customization:
Override default parameters (e.g., sprop for Google):
$link->google(['sprop' => 'name:CustomName']);
Time Zone Handling:
DateTime isn’t timezone-aware.$start = new DateTime('2023-12-25 10:00', new DateTimeZone('America/New_York'));
URL Encoding:
str_replace or urlencode for custom fields:
$link->description(urlencode('Meeting with José & Co.'));
Outlook/Google Quirks:
end time if start is all-day.sprop parameters are optional but can affect rendering.iCal File Generation:
.ics files may not render correctly if headers are missing.Link::create()->ical()->get() to get raw content, then attach to a response:
return response($link->ical()->get(), 200, [
'Content-Type' => 'text/calendar',
'Content-Disposition' => 'attachment; filename="event.ics"',
]);
Validate Links:
Use browser dev tools to inspect generated URLs. Compare against RFC 5545 for iCal.
Example: Check if dates parameter in Google links uses YYYYMMDDTHHmmss format.
Log Raw Output: Temporarily log the generated link to debug:
\Log::debug('Google Link:', ['url' => $link->google()]);
Custom Providers:
Extend the package by creating a new CalendarProvider class:
namespace App\Providers;
use Spatie\CalendarLinks\CalendarProvider;
class CustomCalendar extends CalendarProvider {
public function getUrl(): string {
return 'https://custom-calendar.com/add?event=' . urlencode($this->getEventData());
}
}
Register it in config/calendar-links.php under providers.
Event Data Transformation:
Override getEventData() in a custom class to modify payloads:
class CustomLink extends Link {
public function getEventData(): array {
$data = parent::getEventData();
$data['custom_field'] = 'value';
return $data;
}
}
Testing:
Mock the Link class in tests:
$mockLink = Mockery::mock(Link::class);
$mockLink->shouldReceive('google')->andReturn('https://test.com');
Default Provider:
The package doesn’t enforce a default provider—always chain methods explicitly (e.g., ->google()).
iCal Formatting:
Ensure DateTime objects are in the correct format for iCal (RFC 5545 compliant). Use format('Ymd\THis\Z') if needed.
Attendee Emails: Outlook/Google may validate email formats strictly. Test with real addresses during development.
How can I help you explore Laravel packages today?