F
Filament13mo ago
Arnaud

Enormous class size

Hello, Just wondering if is it me but I'm often reach 1k+ rows of code in my class (resource or page). How to you manage / split your code to keep something clean and readable ? Best regards,
Solution:
For my Resources I will often put my component definitions for Forms and Infolists into a trait that I keep in the same namespaced directory as other things for that Resource (pages, widgets, etc) eg: ```php public static function getProjectNameField()...
Jump to solution
2 Replies
Solution
DrByte
DrByte13mo ago
For my Resources I will often put my component definitions for Forms and Infolists into a trait that I keep in the same namespaced directory as other things for that Resource (pages, widgets, etc) eg:
public static function getProjectNameField()
{
return TextInput::make('project_name')
->label('Project Name')
->maxLength(150)
->required()
->debounce(400)
->afterStateUpdated(function (Forms\Contracts\HasForms $livewire, Forms\Components\TextInput $component) {
// @phpstan-ignore-next-line
$livewire->validateOnly($component->getStatePath());
})
->helperText('(Might include Organization, Building, Landmark, etc.)')
->columnSpanFull();
}
public static function getProjectNameField()
{
return TextInput::make('project_name')
->label('Project Name')
->maxLength(150)
->required()
->debounce(400)
->afterStateUpdated(function (Forms\Contracts\HasForms $livewire, Forms\Components\TextInput $component) {
// @phpstan-ignore-next-line
$livewire->validateOnly($component->getStatePath());
})
->helperText('(Might include Organization, Building, Landmark, etc.)')
->columnSpanFull();
}
public function form(Form $form): Form
{
return $form->schema([
static::getProjectNameField(),

// or
OtherResourceName::getProjectNameField(),

// etc
]);
}
public function form(Form $form): Form
{
return $form->schema([
static::getProjectNameField(),

// or
OtherResourceName::getProjectNameField(),

// etc
]);
}
That way if I need to re-use it in another resource (such as something that's used in both an Admin panel and in a non-Admin panel) I can just re-use the same definition in both places. This keeps it consistent with things like validation and field-lengths that match the db, helper text, etc. If I need to customize something about it, I can chain additional methods on it in the Resource.
Arnaud
ArnaudOP13mo ago
Thank you, I ended up to similar approach 🙂

Did you find this page helpful?