Add when() method to Fields #4107
Answered
by
crynobone
fschirinzi
asked this question in
Q&A
-
|
I think it would be a nice feature to have the Currently, when I conditionally want to set field settings, I have to create the field outside of the array and reference to it. Example NOW: class MyResource extends Resource
{
public function fields(NovaRequest $request)
{
$belongsToUserField = BelongsTo::make('User')->hideWhenCreating()->readonly();
if ($request->viaResource()) {
$belongsToUserField->show();
}
return [
$belongsToUserField,
]
}
}Example with class MyResource extends Resource
{
public function fields(NovaRequest $request)
{
return [
BelongsTo::make('User')
->hideWhenCreating()
->readonly()
->when($request->viaResource(), fn($field) => $field->show()),
]
}
}The second example is much nicer and cleaner. 🤗 |
Beta Was this translation helpful? Give feedback.
Answered by
crynobone
May 16, 2022
Replies: 1 comment
-
|
Field already implements |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
crynobone
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Field already implements
Macroable, you should be able to add the macro for your purpose.