Courses

Laravel 12 Multi-Tenancy: All You Need To Know

Saving Invitation Data in DB: Model, Migration, Controller, Validation

You're reading a FREE PREVIEW of a PREMIUM course.
Summary of this lesson:
- Creating Invitation model and migration
- Building invitation form interface
- Implementing invitation validation
- Displaying pending invitations list

To create the invitation system, we need to save the invitations themselves in the database.


Model and DB Fields

So, let's create a Model with Migration.

php artisan make:model Invitation -m

For the invitation, we need these fields:

  • Tenant ID to know for which tenant user is being invited
  • Email
  • Some random token for security
  • And whether it was accepted

database/migrations/xxx_create_invitations_table.php:

Schema::create('invitations', function (Blueprint $table) {
$table->id();
$table->foreignId('tenant_id')->constrained();
$table->string('email');
$table->string('token');
$table->timestamp('accepted_at')->nullable();
$table->timestamps();
});

app/Models/Invitation.php:

class Invitation extends Model
{
protected $fillable = [
'tenant_id',
'email',
'token',
'accepted_at',
];
}

Form to Invite Users

Next, we need a form on the user's page with one email input.

resources/views/users/index.blade.php:

<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Users') }}
</h2>
</x-slot>
 
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 bg-white border-b border-gray-200">
Coming soon.
<form method="POST" action="{{ route('users.store') }}">
@csrf
 
<!-- Email Address -->
<div class="mt-4">
<x-input-label for="email" :value="__('Email')" />
<x-text-input id="email" class="block mt-1 w-full" type="email" name="email" :value="old('email')" required autocomplete="username" />
<x-input-error :messages="$errors->get('email')" class="mt-2" />
</div>
 
<div class="mt-4">
<x-primary-button>
{{ __('Send Invitation') }}
</x-primary-button>
</div>
</form>
</div>
</div>
</div>
</div>
</x-app-layout>


Process the Invitation Submit

The form submission will lead to the store() method in the UserController. We will need validation, and we will...

The full lesson is only for Premium Members.
Want to access all 22 lessons of this course? (88 min read)

You also get:

  • 76 courses
  • Premium tutorials
  • Access to repositories
  • Private Discord