After Create Docente, Create User

Hello, this is the context. I have a system where I generate the Teachers resource, then this resource is the one that provides me with the basic CRUD in filament. What I need to do is that when creating the teacher, also creates a user of the filament, I have tried with listeners and functions in the resource create the teacher, but the user is not created, what I have done for now is to make a trigger directly in the database to trigger the creation of the user when a teacher is inserted, however the problem is that the user password is not encrypted and does not allow me to log in to the system if the key is not encrypted. Any ideas on how to do this? Here is my teacher resource code
2 Replies
mr_vjchauhan
mr_vjchauhan2mo ago
"Did you try using this static method when the teacher is created? It automatically triggers:
// Teacher Model
protected static function boot()
{
parent::boot();

static::created(function ($teacher) {
$user = new User();
$user->name = $teacher->name;
$user->email = $teacher->email;
$user->password = Hash::make('default_password'); // Make sure to set a secure default password or prompt for one
$user->save();
});
}
// Teacher Model
protected static function boot()
{
parent::boot();

static::created(function ($teacher) {
$user = new User();
$user->name = $teacher->name;
$user->email = $teacher->email;
$user->password = Hash::make('default_password'); // Make sure to set a secure default password or prompt for one
$user->save();
});
}
Why don't you first create the user and then assign this user to the teacher role?" Maybe I'm wrong
ZamboDev
ZamboDev2mo ago
Thank you! I'm going to try and about creating the user first, the problem is that many teacher fields would be empty, plus this is a migration so I already have more than 200 teachers that I should just migrate and create the user at the same time.