creating a field that'll send an email if checked but that isn't stored anywhere in the DB #1135
-
For my User CRUD I'd like to add a "Send a New User Email with a randomly generated password" checkbox that does exactly what it says. This checkbox would be checked by default but could be unchecked if you stealthily wanted to add a user (eg. maybe you're trying to add an inactive user but you still need a record of them for reporting purposes or whatever). My question is... how would I do this in a CRUD? https://backpackforlaravel.com/docs/6.x/crud-fields#optional-fake-field-attributes-stores-fake-attributes-as-json-in talks about fake fields but those fields seem to be for saving data in a JSON column vs using them to conditionally trigger an action. I mean, I suppose it could be stored in the DB but storing it in the DB seems a bit pointless to me... |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I was able to achieve this by building off of the following link: https://backpackforlaravel.com/docs/6.x/crud-operation-create#override-the-store-method Here's the field I added: CRUD::field([
'name' => 'sendEmail',
'label' => 'Send a <a tabindex="-1" href="/admin/setting/0/edit#new-user-email" target="_blank">New User Email</a> with a randomly generated eight character alpha-numeric password',
'type' => 'checkbox',
'default' => 1,
'attributes' => ['tabindex' => '-1'],
]); And here's my store function: public function store()
{
$request = $this->crud->getRequest();
$email = $request->input('email', '');
$sendEmail = $request->input('sendEmail', '0') == '1';
// do something before validation, before save, before everything
$response = $this->traitStore();
// do something after save
if ($sendEmail) {
$user = User::where('email', $email)->first();
self::sendWelcomeEmail($user);
}
return $response;
} |
Beta Was this translation helpful? Give feedback.
I was able to achieve this by building off of the following link:
https://backpackforlaravel.com/docs/6.x/crud-operation-create#override-the-store-method
Here's the field I added:
And here's my store function: