Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

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

Premium
2:28

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 of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

A
abather ✓ Link copied!

Thank you for this course I have some points :) :

  • why you don't use sperated controller for invitation or create invoke controller to create invitation.
  • in the StoreUserRequest Rule I think the email should not be unique beacuse the user can be invited to many tenat, more than that even if the user is registerd before he may get invitation to other tenat 'I think it is covered on the next topics', so I think it well be beter if we make ['tenat_id', 'email'] unique.
N
Nerijus ✓ Link copied!

Yes it can be how you say, but every application is unique. What works for one doesn't mean it will be best for other. This is only a tutorial, a starting point to build application.