Laravel Invite Only Adds a Full User Invitation System with Tokens, Events, and Reminders
Published on by Yannick Lyn Fatt
The Laravel Invite Only package manages user invitations in Laravel applications. Created by Shavonn Brown, this package handles token-based invitations with polymorphic relationships, automatic reminders, and event-driven notifications.
Use Cases
The package works for applications that need invitation management with status tracking and follow-ups. Common scenarios include team collaboration tools, SaaS applications with organization memberships, project collaboration platforms, event management systems with RSVP tracking, and educational platforms that invite students to courses. The polymorphic design means one invitation system can handle teams, projects, organizations, or any other model in your application.
Main Features
Laravel Invite Only includes several features for managing invitations:
- Polymorphic invitations that work with any model in your application
- Bulk invitation support with partial failure handling
- Secure token-based links for invitation acceptance
- Status tracking across pending, accepted, declined, expired, and cancelled states
- Automatic reminder emails via scheduled commands
- Event system for all invitation lifecycle changes
- Structured exceptions with error codes for debugging
Getting Started
Installation requires PHP 8.2+ and Laravel 11.0 or 12.0. Install the package via Composer:
composer require offload-project/laravel-invite-only
Publish the configuration and migrations:
php artisan vendor:publish --tag="invite-only-config"php artisan vendor:publish --tag="invite-only-migrations"php artisan migrate
Then, add the required HasInvitations and CanBeInvited traits to your models:
use OffloadProject\InviteOnly\Traits\HasInvitations; class Event extends Model{ use HasInvitations;}
use OffloadProject\InviteOnly\Traits\CanBeInvited; class User extends Authenticatable{ use CanBeInvited;}
Sending Invitations
The package supports both single and bulk invitations. For a single invitation:
$event->invite('user@example.com', [ 'role' => 'vip', 'invited_by' => auth()->user(),]);
For bulk invitations with partial failure handling:
$result = $event->inviteMany( ['eric@example.com', 'paul@example.com', 'harris@example.com'], ['role' => 'vip', 'invited_by' => auth()->user()]); // Access successful invitations$result->successful; // Check failed invitations with reasons$result->failed;
Handling Invitation Acceptance
The package fires events at each stage of the invitation lifecycle. Listen for the InvitationAccepted event to perform actions when users accept invitations:
use OffloadProject\InviteOnly\Events\InvitationAccepted; Event::listen(InvitationAccepted::class, function ($event) { $conferenceEvent = $event->invitation->invitable; $user = $event->user; $role = $event->invitation->role; $conferenceEvent->attendees()->attach($user->id, ['role' => $role]);});
Other invitation events include InvitationCreated, InvitationDeclined, InvitationCancelled, and InvitationExpired.
Automatic Reminders
Configure automatic reminders to send follow-up emails for pending invitations. The configuration allows you to specify which days reminders should be sent:
'reminders' => [ 'after_days' => [3, 5], // Send reminders on day 3 and day 5 'max_reminders' => 2,],
Schedule the reminder command in your routes/console.php file:
use Illuminate\Support\Facades\Schedule; Schedule::command('invite-only:send-reminders --mark-expired')->daily();
The system tracks the reminder count to avoid sending duplicate notifications, even if the scheduler misses a day. Invitations can be configured to expire after a specified number of days (7 days by default), and the same command automatically marks expired invitations when running with the --mark-expired flag.
Polymorphic Relationships
One of the package's strengths is its polymorphic design. You can attach invitations to any model in your application:
// Conference event invitations$conferenceEvent->invite('speaker@example.com'); // Workshop invitations$workshop->invite('attendee@example.com'); // Meetup invitations$meetup->invite('participant@example.com');
This works for applications with multiple types of entities that require invitation systems.
Custom Metadata
Store additional information with invitations using the metadata column:
$event->invite('speaker@example.com', [ 'metadata' => [ 'session_topic' => 'Laravel AI SDK', 'time_slot' => '2:00 PM', 'dietary_restrictions' => 'vegetarian', ],]); // Retrieve metadata later$invitation->metadata['session_topic']; // 'Laravel Performance'
This allows you to attach custom data without modifying the database schema.
Error Handling
The package provides structured exceptions with machine-readable error codes:
try { InviteOnly::accept($token);} catch (InvitationException $e) { // Human-readable message $e->getMessage(); // "This invitation has expired." // Suggested resolution $e->resolution; // "Create a new invitation..." // Machine-readable code $e->errorCode; // "INVITATION_EXPIRED"}
This makes it easier to build API endpoints and provide meaningful feedback to users.
To learn more about Laravel Invite Only and view the source code, visit the GitHub repository.