F
Filament14mo ago
ico

Filament v3 Resource does not fill the Edit form

Hello i integrated the Filament package into a existing Laravel 10 project. When i followed the guide for the panels i discovered that after i have created a Resource ( when i filled the logic for the Table ) it doesn't fill the Edit form with the selected record. My table looks like this (from the docs and demo on github )
->columns([
Tables\Columns\TextColumn::make('name'),
Tables\Columns\TextColumn::make('defectCategory.weight'),
Tables\Columns\TextColumn::make('')->label('Check type')
->description(fn (Defect $defect): string => $defect->qualityCheck)
])
->filters([
//
])
->actions([
EditAction::make(),
])
->emptyStateActions([
CreateAction::make(),
]);
->columns([
Tables\Columns\TextColumn::make('name'),
Tables\Columns\TextColumn::make('defectCategory.weight'),
Tables\Columns\TextColumn::make('')->label('Check type')
->description(fn (Defect $defect): string => $defect->qualityCheck)
])
->filters([
//
])
->actions([
EditAction::make(),
])
->emptyStateActions([
CreateAction::make(),
]);
And my Edit page is almost blank
protected static string $resource = DefectResource::class;
protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
];
}
protected static string $resource = DefectResource::class;
protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
];
}
Was looking around found some were doing with the ->record() method so i tried
EditAction::make()
->record(function ($record) {
return Defect::find($record);
}),
EditAction::make()
->record(function ($record) {
return Defect::find($record);
}),
But didn't make a difference So what do i need to do to fill my Edit form with the selected record ?
15 Replies
Quin.
Quin.14mo ago
You need to fill in your form in your resource if i am correct
Quin.
Quin.14mo ago
https://filamentphp.com/docs/3.x/forms/fields/getting-started if you use the textInput for your name , then you could see the name in the edit
ico
icoOP14mo ago
i do have the make() method on my text inputs in the form
->schema([
// create form
Forms\Components\Section::make()
->schema([
Forms\Components\TextInput::make('defect')
->required()
->maxLength(255)
->label(__()),
Forms\Components\Select::make('categoryId')->label('Category')
->required()
->options(
DefectCategory::fillamentElements() // custom method in the class
),
Forms\Components\Select::make('checkTypeId')->label("Check Type")
->required()
->options(
Defect::getQualityCheck(),
)
])
->columns(3)
->columnSpan(['lg' => fn (?Defect $record) => $record === null ? 3 : 2]),
// Show additional Section if a record is selected
Forms\Components\Section::make()
->schema([
Forms\Components\Placeholder::make('created_at')
->content(fn (Defect $record): ?string => $record->created_at?->diffForHumans()),
Forms\Components\Placeholder::make('updated_at')
->content(fn (Defect $record): ?string => $record->updated_at?->diffForHumans()),
])
->columnSpan(['lg' => 1])
->hidden(fn (?Defect $record) => $record === null),
])
->schema([
// create form
Forms\Components\Section::make()
->schema([
Forms\Components\TextInput::make('defect')
->required()
->maxLength(255)
->label(__()),
Forms\Components\Select::make('categoryId')->label('Category')
->required()
->options(
DefectCategory::fillamentElements() // custom method in the class
),
Forms\Components\Select::make('checkTypeId')->label("Check Type")
->required()
->options(
Defect::getQualityCheck(),
)
])
->columns(3)
->columnSpan(['lg' => fn (?Defect $record) => $record === null ? 3 : 2]),
// Show additional Section if a record is selected
Forms\Components\Section::make()
->schema([
Forms\Components\Placeholder::make('created_at')
->content(fn (Defect $record): ?string => $record->created_at?->diffForHumans()),
Forms\Components\Placeholder::make('updated_at')
->content(fn (Defect $record): ?string => $record->updated_at?->diffForHumans()),
])
->columnSpan(['lg' => 1])
->hidden(fn (?Defect $record) => $record === null),
])
So i need to add the name() method also ?
Quin.
Quin.14mo ago
you do return the $form?
ico
icoOP14mo ago
yes
Quin.
Quin.14mo ago
hmmm thats a good one man, You don't need to add the name it isn't required. But it does create your records?
ico
icoOP14mo ago
hmmm it flashesh but it doesn't create the record in the DB
Dennis Koch
Dennis Koch14mo ago
If this is an EditPage in a Panel you don't need to do anything.
ico
icoOP14mo ago
it is i created a new AppPanleProvider and when i typed the command for a new resource it asked me for witch panel i wanted the resource to be so i did select the app panel provider that i created and all i did after was to fill the $table and the $form
Dennis Koch
Dennis Koch14mo ago
Anything special about your Defect model? Can you share that code?
ico
icoOP14mo ago
nothing special
class Defect extends Model
{
use HasFactory, SoftDeletes, Varied;

protected $guarded = [];

public static function getQualityCheck(): Collect
{
return collect([
1 => 'Visual',
2 => 'Metric',
]);
}

public function getQualityCheckAttribute(): strin
{
return self::getQualityCheck()[$this->checkTypeId];
}



Relationships to other tables
..........
class Defect extends Model
{
use HasFactory, SoftDeletes, Varied;

protected $guarded = [];

public static function getQualityCheck(): Collect
{
return collect([
1 => 'Visual',
2 => 'Metric',
]);
}

public function getQualityCheckAttribute(): strin
{
return self::getQualityCheck()[$this->checkTypeId];
}



Relationships to other tables
..........
This is my AppPanelProvider
$panel
->id('app')
->path('filament')
->discoverResources(in: app_path('Filament/App/Resources'), for: 'App\\Filament\\App\\Resources')
->discoverPages(in: app_path('Filament/App/Pages'), for: 'App\\Filament\\App\\Pages')
->pages([
ListDefects::class
])
->discoverWidgets(in: app_path('Filament/App/Widgets'), for: 'App\\Filament\\App\\Widgets')
->navigationGroups([
'Quality'
])
->middleware([
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
DisableBladeIconComponents::class,
DispatchServingFilamentEvent::class,
])
->authMiddleware([
Authenticate::class,
]);
$panel
->id('app')
->path('filament')
->discoverResources(in: app_path('Filament/App/Resources'), for: 'App\\Filament\\App\\Resources')
->discoverPages(in: app_path('Filament/App/Pages'), for: 'App\\Filament\\App\\Pages')
->pages([
ListDefects::class
])
->discoverWidgets(in: app_path('Filament/App/Widgets'), for: 'App\\Filament\\App\\Widgets')
->navigationGroups([
'Quality'
])
->middleware([
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
DisableBladeIconComponents::class,
DispatchServingFilamentEvent::class,
])
->authMiddleware([
Authenticate::class,
]);
Dennis Koch
Dennis Koch14mo ago
Is Varied doing anything special?
ico
icoOP14mo ago
no it for generating the data in the DB it has nothing to do with filament
Dennis Koch
Dennis Koch14mo ago
Can you make it work with any other model? Just to make sure
ico
icoOP14mo ago
haven't tested with other resources yet
Want results from more Discord servers?
Add your server