Send email after register user

Hi guys, I have a simple user registration form to input the name and email, and I need that when the user registers, an email is automatically sent to reset the password. I have this form. I'm using afterCreate hook with this: protected function afterCreate(): void { $user = $this->record; $token = app('auth.password.broker')->createToken($user); $notification = new ResetPassword($token); $notification->url = Filament::getResetPasswordUrl($token, $user); $user->notify($notification); } But I receive Field 'password' doesn't have a default value. Have I save a random value before to create user?
No description
17 Replies
krekas
krekas15mo ago
When creating user password cannot be null which seems to be in your case
Daniel Reales
Daniel RealesOP15mo ago
I know, but for this case I need to set ->nullable() on migration or existe another case use?
krekas
krekas15mo ago
Set a random password when creating a user
Daniel Reales
Daniel RealesOP15mo ago
I did: TextInput::make('password')->required()->password()->default(Hash::make(Str::random(13)))->hidden(), But I receive the same error
krekas
krekas15mo ago
Hidden is the problem
Daniel Reales
Daniel RealesOP15mo ago
Can this field be sent even if it has the 'hidden' value?
krekas
krekas15mo ago
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
krekas
krekas15mo ago
And stop replying like that to every message Hidden field can't be saved to db and it's a security concern
Daniel Reales
Daniel RealesOP15mo ago
Ah ok thank you sorry
krekas
krekas15mo ago
You need to mutatedatabeforesave
Daniel Reales
Daniel RealesOP15mo ago
I could do this beforeCreate hooks?
krekas
krekas15mo ago
Use your ide to find correct method
Daniel Reales
Daniel RealesOP15mo ago
thank you for your help!
Panda
Panda15mo ago
You can also use Eloquent events by hooking into the creating event and setting a random password if none is provided.
class User extends Authenticatable implements FilamentUser {
// ...

/**
* The "booted" method of the model.
*/
protected static function booted(): void
{
static::creating(function (User $user) {
$user->password = $user->password ?: Str::random();
});
}
}
class User extends Authenticatable implements FilamentUser {
// ...

/**
* The "booted" method of the model.
*/
protected static function booted(): void
{
static::creating(function (User $user) {
$user->password = $user->password ?: Str::random();
});
}
}
Daniel Reales
Daniel RealesOP15mo ago
It's possible to send Filament::getResetPasswordUrl($token, $user); to an specific panel? I'm creating new users from admin panel but I want to send an email to users with client panel link
Panda
Panda15mo ago
I've not used it but digging into the code got me the following snippet. Check if it meets your needs or was it a dumb call.
Filament::getPanel('admin')->getResetPasswordUrl()
Filament::getPanel('admin')->getResetPasswordUrl()
You can pass the same parameters to this method. The definition is similar to Filament::getResetPasswordUrl.
public function getResetPasswordUrl(string $token, CanResetPassword | Model | Authenticatable $user, array $parameters = []): string
{}
public function getResetPasswordUrl(string $token, CanResetPassword | Model | Authenticatable $user, array $parameters = []): string
{}
Daniel Reales
Daniel RealesOP15mo ago
It's exactly what I was looking for! Thank you so much!!
Want results from more Discord servers?
Add your server