hide field conditionally and submit values accordingly.

Hi there! I wan to achieve something like if user has role admin then show this
Select::make('user_id')
->options(
User::with('roles')->whereHas('roles', function ($query) { $query->where('name', 'manager');
)->pluck('name','id')
)->searchable()
->label('Managers')
Select::make('user_id')
->options(
User::with('roles')->whereHas('roles', function ($query) { $query->where('name', 'manager');
)->pluck('name','id')
)->searchable()
->label('Managers')
otherwise i want to submit default value like this
Hidden::make('user_id')
->default(auth()->id())
Hidden::make('user_id')
->default(auth()->id())
how to achieve that? Thanks!
7 Replies
ChesterS
ChesterS13mo ago
Don't use Hidden to submit data. You can have an if condition to show the Select
$fields = [
...
];
if($user->isAdmin()) {
$fields[] = [ Select::make()
}

return $fields;
$fields = [
...
];
if($user->isAdmin()) {
$fields[] = [ Select::make()
}

return $fields;
and do something similar in the mutateFormDataUsing() method to add a default value eg
->mutateFormDataUsing(function ($data) {
$data['user_id'] = auth()->id();

return $data;
})
->mutateFormDataUsing(function ($data) {
$data['user_id'] = auth()->id();

return $data;
})
Not sure if ther's another way You can also use ->hidden() but that does render some stuff. However, AFAIK, hidden fields are not hydrated automatically so it's probably safe?
Dhaval Kacha
Dhaval KachaOP13mo ago
Hey, Thank you for your help, I achieved it like this.
if (auth()->user()->hasRole('admin')) {
$field = Select::make('user_id')
->options(
User::with('roles')->whereHas('roles',
function ($query) {
$query->where('name', 'manager');
})->pluck('name','id')
)
->searchable()
->label('Managers');
} else {
$field = Hidden::make('user_id')
->default(auth()->id());
}
if (auth()->user()->hasRole('admin')) {
$field = Select::make('user_id')
->options(
User::with('roles')->whereHas('roles',
function ($query) {
$query->where('name', 'manager');
})->pluck('name','id')
)
->searchable()
->label('Managers');
} else {
$field = Hidden::make('user_id')
->default(auth()->id());
}
and used this $field variable inside schema
Fieldset::make('')
->schema([
$field,
OtherFields...,
])
Fieldset::make('')
->schema([
$field,
OtherFields...,
])
Dennis Koch
Dennis Koch13mo ago
First thing Chester said was:
Don't use Hidden to submit data.
It's a security risk. Why didn't you use his example?!
Dhaval Kacha
Dhaval KachaOP13mo ago
Alright! where to write ->mutateFormDataUsing?
->mutateFormDataUsing(function ($data) {
$data['user_id'] = auth()->id();

return $data;
})
->mutateFormDataUsing(function ($data) {
$data['user_id'] = auth()->id();

return $data;
})
Dennis Koch
Dennis Koch13mo ago
What chester showed was an example for Actions. Depending on whether you have a modal or a separate page
Dhaval Kacha
Dhaval KachaOP13mo ago
Oh, I have used this method mutateFormDataBeforeCreate() before. Its really great alternative for Hidden or to submit default value. Now my code looks like this.
Fieldset::make('')
->schema([
Select::make('user_id')
->options(
User::with('roles')->whereHas('roles',
function ($query) {
$query->where('name', 'manager');
})->pluck('name','id')
)
->searchable()
->label('Managers')
->hidden(auth()->user()->hasRole('manager')),
])
Fieldset::make('')
->schema([
Select::make('user_id')
->options(
User::with('roles')->whereHas('roles',
function ($query) {
$query->where('name', 'manager');
})->pluck('name','id')
)
->searchable()
->label('Managers')
->hidden(auth()->user()->hasRole('manager')),
])
I've removed if else and added hidden for manager role and adding default value for manager role. Thank you @Dennis Koch , @ChesterS
Want results from more Discord servers?
Add your server