F
Filament9mo ago
torriv

Possible to extract common fields?

Just wondering if i'm doing it right, or if there is a way to optimize it somehow. I feel it's a lot of repeating. any suggestions are welcome. i have a form where i put each day in a week in a tab. so the tabs are named 'Monday', 'Tuesday' and so on. Under each of these tabs, i'm adding data for that day.

Tabs\Tab::make('Mandag')
->badge(fn($record) => WeekplanExercise::where('day', 1)->where('weekplan_id', $record?->id)->count())
->schema([
Repeater::make('exercises_1')
->relationship('weekplanExercises', function ($query) {
$query->where('day', 1);
})
->label('Økter')
->schema([
Hidden::make('day')->default('1'),
Select::make('exercise_id')
//more code
Select::make('training_program_id')
//more code
TimePicker::make('start_time')
//more code
TimePicker::make('end_time')
//more code
Select::make('intensity')->options([
'green' => 'Lett',
'darkcyan' => 'Vedlikehold',
'crimson' => 'Tung',
])
->label('Hvor tung?')
->required(),
])
->defaultItems(0)
->grid(4)
->itemLabel(
fn(array $state): ?string => Exercise::all()->where('id',
$state['exercise_id'])->first()?->name
)
->addActionLabel('Legg til økt')
->collapsible(),
]),

Tabs\Tab::make('Mandag')
->badge(fn($record) => WeekplanExercise::where('day', 1)->where('weekplan_id', $record?->id)->count())
->schema([
Repeater::make('exercises_1')
->relationship('weekplanExercises', function ($query) {
$query->where('day', 1);
})
->label('Økter')
->schema([
Hidden::make('day')->default('1'),
Select::make('exercise_id')
//more code
Select::make('training_program_id')
//more code
TimePicker::make('start_time')
//more code
TimePicker::make('end_time')
//more code
Select::make('intensity')->options([
'green' => 'Lett',
'darkcyan' => 'Vedlikehold',
'crimson' => 'Tung',
])
->label('Hvor tung?')
->required(),
])
->defaultItems(0)
->grid(4)
->itemLabel(
fn(array $state): ?string => Exercise::all()->where('id',
$state['exercise_id'])->first()?->name
)
->addActionLabel('Legg til økt')
->collapsible(),
]),
This code is the same for each day, except the tab name, badge, repeater make() and relationship query. Would it be possible to extract the common parts of this somehow? hope you understand what i'm asking for.
Solution:
In my case, I created a Trait to share between a few resources, and in the trait are static methods like getAuthorNameField() or getMetaSection() to easily call those methods when defining my form schema in multiple resources. This keeps things consistent, and I can re-order or keep/exclude things in various resources according to other requirements, without making it hard to maintain. Thus my form(Form $form) method simply defines the Grid and Sections schema, and then inside the Sections I call the various getFoo() methods from my trait. In some cases I call complete sections via a getFooSection() method in my trait, where I always use the same form elements when using that section. I suppose you could pass methods to those functions too, to may them slightly dynamic if needed. In my case they all relate to the same Model, so don't require anything dynamic at the per-field level. ...
Jump to solution
2 Replies
Solution
DrByte
DrByte9mo ago
In my case, I created a Trait to share between a few resources, and in the trait are static methods like getAuthorNameField() or getMetaSection() to easily call those methods when defining my form schema in multiple resources. This keeps things consistent, and I can re-order or keep/exclude things in various resources according to other requirements, without making it hard to maintain. Thus my form(Form $form) method simply defines the Grid and Sections schema, and then inside the Sections I call the various getFoo() methods from my trait. In some cases I call complete sections via a getFooSection() method in my trait, where I always use the same form elements when using that section. I suppose you could pass methods to those functions too, to may them slightly dynamic if needed. In my case they all relate to the same Model, so don't require anything dynamic at the per-field level. I suppose you could do similar as I did with Sections if you had common things per "Day". And I suppose you could have some of the functions in the trait call other functions to render a group of things based on various criteria. And maybe a Trait is too rigid, and another kind of class would be fine for your needs. There are lots of options you could explore.
torriv
torriv9mo ago
Thanks! I'll try that out! 🙂 Just wanted to give you a feedback. Using trait was exactly what I needed. This opens a new world and ideas for me. Thank you!