F
Filament15mo ago
Xe

Make the Current user of the panel as an input to the form

Hello, I'm making something like: the admin puts information for the users, The information should be marked in my db as that admin is the one who wrote it, How should I do that?
9 Replies
Vp
Vp15mo ago
Foreign id for admin like 'created_by'
Xe
XeOP15mo ago
yeah It's done in the model and the db all good, but the form that I should fill requires typing the email of the current admin, I don't want that, I want it to automatically bring it from the app
Vp
Vp15mo ago
For this I have 2 options, you can choose whatever you like. 1. Mutate form before creating https://filamentphp.com/docs/2.x/admin/resources/creating-records#customizing-data-before-saving 2. Laravel Observer https://laravel.com/docs/10.x/eloquent#observers
Xe
XeOP15mo ago
if I precise more, here
->schema([
Forms\Components\TextInput::make('admin')
->hidden()
-
->required()
->maxLength(255),
->schema([
Forms\Components\TextInput::make('admin')
->hidden()
-
->required()
->maxLength(255),
this is the nput I want here instead of this, to make a function that brings the user email, the function is already done, it just returns the email from the model but how to assign it as an input 🥲
Vp
Vp15mo ago
I don't understand what you're trying..
Lara Zeus
Lara Zeus15mo ago
if admin is a relation in the model you can do admin.email
Xe
XeOP15mo ago
now I'm doing this
protected function mutateFormDataBeforeCreate(array $data): array
{
$data['admin'] = auth()->email();

return $data;
}
protected function mutateFormDataBeforeCreate(array $data): array
{
$data['admin'] = auth()->email();

return $data;
}
In the create page, but that's obv wrong, how can I get the email here? yes, but how to input that?
Lara Zeus
Lara Zeus15mo ago
this should work $data['admin'] = auth()->user()->email;
Xe
XeOP15mo ago
Yess, Worked!! Thank you @Vp @Lara Zeus
Want results from more Discord servers?
Add your server