Skip to main content

Employee User Invitations Process

Premium
6 min read

This lesson got separated as it's a crucial part of the application - sending out an invitation email to an employee and allowing them to register to the system:

In this lesson, we will do the following:

  • Create Invitation Model and Database tables
  • Modify UserResource Create button action - to invite the Employee
  • Email the invitation to the Employee
  • Create a custom page that will be signature (Laravel Signer URL) protected
  • Create a custom registration form for the Employee

Create Invitation Model and Database tables

Let's create our migration:

Migration

Schema::create('invitations', function (Blueprint $table) {
$table->id();
$table->string('email');
$table->timestamps();
});

Then, we can fill our Model:

app/Models/Invitation.php

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

As you can see from the setup, it's a pretty basic Model. All we care about - is the email address being invited.


Modify UserResource Create Button Action - to Invite the Employee

Next on our list, we need to modify the User Create button. We don't want to create...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (30 h 50 min)

You also get:

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

Already a member? Login here

Comments & Discussion

ED
Ervalsa Dwi Nanda ✓ Link copied!

i have finish register from invitation email. but when registrating it cannot move to admin/login route and the email_verfied_at and remember_token is null so the invited account cannot used to login

M
Modestas ✓ Link copied!

In this tutorial we did not talk about the email verification, so you can add that manually.

remember me token - that does not matter, no? And for the redirect - I'm not sure what you mean

ED
Ervalsa Dwi Nanda ✓ Link copied!

Thanks for you answer sir.

ED
Ervalsa Dwi Nanda ✓ Link copied!

Sir, i have problem when it finish sign up, it show route [login] not defined

M
Modestas ✓ Link copied!

Could you expand on this issue? I'm not sure what the problem is

WC
WILLY CHANDRA NEGARA ✓ Link copied!

Sir, i have problem when it finish sign up, it show route [login] not defined

You should sign up in different browser or just use incognito mode.

ES
Erik Slooff ✓ Link copied!

Nice approach; I've been thinking about this subject for some time now and quite like your take.

Is there a reason you used a Mailable instead of a Notification (which I personally prefer)?

I'll also be looking to implement role selection when creating the invitation and making the invitation expire after a certain period, either by using temporarySignedRoute() or by setting timestamp in a column in the invitations table.

M
Modestas ✓ Link copied!

Mailable was used since we don't have an active user (I personally like to separate Notifications (user in system can receive them) and Mailables (users not in system get them)). Other than that - you can change it how you like it :)

As for the role - you can add that to invitations table for sure! That was something we specifically skipped as it depends. Everyone wants the proccess a little bit different :)

ES
Erik Slooff ✓ Link copied!

Thanks, yes that totally makes sense.

ES
Erik Slooff ✓ Link copied!

There appears to be a bug in this example; in AcceptInvitation.php you are Hashing the password twice. Once in the Field configuration and another time when you create the new User instance.

M
Modestas ✓ Link copied!

Oops! Updating the lesson :)

ES
Erik Slooff ✓ Link copied!

Thanks, I was struggling to understand why the password didn't match until I discovered what was going on:wink:

M
Majd ✓ Link copied!

I keep getting this error: any ideas? Target [Filament\Forms\Contracts\HasForms] is not instantiable while building [Filament\Forms\Form].

M
Modestas ✓ Link copied!

Could you expand on this a bit more? I'm not sure what you were doing to get this error.

ps. Do not cross post on multiple articles ;)

S
Sjoerd24 ✓ Link copied!

In the file /app/Livewire/AcceptInvitation.php the line all the way on top:

namespace App\Livewire;

is missing. Maybe logical but I would add it for clarity. Great example btw, very useful for my project!

Edit: Wait and one other thing that i noticed, make sure that there is no identation in the blade (resources/views/emails/team-invitation.blade.php) of the email, markdown sees that as formatting. That all has to be removed.

SI
Solomon Iroegbu ✓ Link copied!

im getting this error when i try to invite a user.

Connection could not be established with host "mailpit:1025": stream_socket_client(): php_network_getaddresses: getaddrinfo for mailpit failed: No such host is known.

M
Modestas ✓ Link copied!

This error means that you have misconfigured your .env MAIL settings. Please check them and make sure they are correct

NM
Nicolas Menescardi ✓ Link copied!

In here:

return Action::make('register')
->label(__('filament-panels::pages/auth/register.form.actions.register.label'))
->submit('register')

Is it possible to disable the submit button after submittion? Thanks!

IA
Ihtisham Ahmad ✓ Link copied!

Property [$form] not found on component: [accept-invitation] I'm getting this error.

M
Modestas ✓ Link copied!

This usually means that the accept-invitation page is incorrectly set up. Double check that everything is there

RS
Rafał Skonieczka ✓ Link copied!

I have the same :(

Unable to find component: [app.http.livewire.accept-invitation]

O
optimalspieler ✓ Link copied!

Great tutorial! How can I change the primary color, since the form is outside of my Filament panel?

->colors([
'primary' => Color::hex('#990000'),
])
O
optimalspieler ✓ Link copied!

I think I have figured it out:

https://filamentphp.com/docs/3.x/support/colors

Add this to the AppServiceProvider

public function boot(): void
{
FilamentColor::register([
'primary' => Color::hex('#990000'),
]);
}
JJ
Jeff Jeong ✓ Link copied!

hello thanks for the great tutorial! but i have some issue on email formatting

I have no idea how to solve this.

<table class="action" align="center" width="100%" cellpadding="0" cellspacing="0" role="presentation">

in email format Constanly getting this some <table line Do you have some suggestion??

It would be very appreciated! thanks!

M
Modestas ✓ Link copied!

I'm not sure what you have in mind here, sorry

B
bogusbd ✓ Link copied!

use this teplate for resources/views/emails/team-invitation.blade.php

without double line breaks

<x-mail::message>
You have been invited to join {{ config('app.name') }}
To accept the invitation - click on the button below and create an account:
<x-mail::button :url="$url">
{{ __('Create Account') }}
</x-mail::button>
{{ __('If you did not expect to receive an invitation to this team, you may discard this email.') }}
</x-mail::message>

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.