Tinkerwell - The PHP Scratchpad

Laravel Invite Only Adds a Full User Invitation System with Tokens, Events, and Reminders

Published on by

Laravel Invite Only Adds a Full User Invitation System with Tokens, Events, and Reminders image

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.

Yannick Lyn Fatt photo

Staff Writer at Laravel News and Full stack web developer.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Bacancy

Outsource a dedicated Laravel developer for $3,200/month. With over a decade of experience in Laravel development, we deliver fast, high-quality, and cost-effective solutions at affordable rates.

Visit Bacancy
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $3200/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit

The latest

View all →
Generate Secure, Memorable Passphrases in PHP with PHP Passphrase image

Generate Secure, Memorable Passphrases in PHP with PHP Passphrase

Read article
FrankenPHP v1.11.2 Released With 30% Faster CGO, 40% Faster GC, and Security Patches image

FrankenPHP v1.11.2 Released With 30% Faster CGO, 40% Faster GC, and Security Patches

Read article
Capture Web Page Screenshots in Laravel with Spatie's Laravel Screenshot image

Capture Web Page Screenshots in Laravel with Spatie's Laravel Screenshot

Read article
Nimbus: An In-Browser API Testing Playground for Laravel image

Nimbus: An In-Browser API Testing Playground for Laravel

Read article
Laravel 12.51.0 Adds afterSending Callbacks, Validator whenFails, and MySQL Timeout image

Laravel 12.51.0 Adds afterSending Callbacks, Validator whenFails, and MySQL Timeout

Read article
Handling Large Datasets with Pagination and Cursors in Laravel MongoDB image

Handling Large Datasets with Pagination and Cursors in Laravel MongoDB

Read article