Toggle without mass assignement

Hi guys, I want to add toggle to my UserResource and use value from it in afterCreate hook to generate and send password via email. But I dont want to have this value from toggle saved on model.
// UserResource.php
Forms\Components\Toggle::make('send_credentials')
->label(__('Send user email with credentials'))
->visibleOn('create'),
// UserResource.php
Forms\Components\Toggle::make('send_credentials')
->label(__('Send user email with credentials'))
->visibleOn('create'),
// CreateUser.php
public function afterCreate(): void
{
ray($this->send_credentials);
}
// CreateUser.php
public function afterCreate(): void
{
ray($this->send_credentials);
}
How can I prevent this error: Add fillable property [send_credentials] to allow mass assignment on [App\Models\User]. and access that property on afterCreate hook?
8 Replies
Trauma Zombie
Trauma Zombie4mo ago
Thank you. Can you help me with second part of this issue? I want to generate random password. It should be generated probably in beforeCreate, but mail should be send in afterCreate. Or should I send that email also in beforeCreate?
Vp
Vp4mo ago
Wait, my answer will be incorrect.. I don't think you can access send_credentials in ->afterCreate() if you don't save in DB.
Trauma Zombie
Trauma Zombie4mo ago
Can I access it in beforeCreate?
Vp
Vp4mo ago
Can you try out first.. I am not sure both 🤣 For password, you can store in variable, and use in both create and after create..
Trauma Zombie
Trauma Zombie4mo ago
In constructor?
Vp
Vp4mo ago
No, something like this
public $randomPassword = Str::random(12);

protected function mutateFormDataBeforeCreate(array $data): array
{
$data['password'] = bcrypt($this->randomPassword);

return $data;
}

protected function afterCreate()
{
send_email($this->randomPassword);
}
public $randomPassword = Str::random(12);

protected function mutateFormDataBeforeCreate(array $data): array
{
$data['password'] = bcrypt($this->randomPassword);

return $data;
}

protected function afterCreate()
{
send_email($this->randomPassword);
}
Trauma Zombie
Trauma Zombie4mo ago
I did it like this:
class CreateUser extends CreateRecord
{
protected static string $resource = UserResource::class;

protected string $password;
protected bool $sendCredentials;

protected function mutateFormDataBeforeCreate(array $data): array
{
$this->password = str()->random(12);
$this->sendCredentials = $data['send_credentials'];

unset($data['send_credentials']);

$data['password'] = bcrypt($this->password);

return $data;
}

public function afterCreate(): void
{
ray($this->sendCredentials);
}
}
class CreateUser extends CreateRecord
{
protected static string $resource = UserResource::class;

protected string $password;
protected bool $sendCredentials;

protected function mutateFormDataBeforeCreate(array $data): array
{
$this->password = str()->random(12);
$this->sendCredentials = $data['send_credentials'];

unset($data['send_credentials']);

$data['password'] = bcrypt($this->password);

return $data;
}

public function afterCreate(): void
{
ray($this->sendCredentials);
}
}
I have a hunch that there is a better solution, but I haven't figured it out.