Can not use $operation utility injection in schema closure
I am getting this error: An attempt was made to evaluate a closure for [Filament\Schemas\Schema], but [$operation] was unresolvable.
I want to check if the page / operation is an edit or create
return $schema
->components(function (?Model $record, string $operation) {
$entryArray = [];
if ($operation === Operation::Edit->value) {
$counter = 1;
foreach ($record->files as $file) {
$entryArray[] = ImageEntry::make('advertisement.file.id.' . $counter)->label('file ' . $counter)->state($file->url);
$counter++;
}
}
$components = [
TranslationComponent::configure('title')
->required(),
TranslationComponent::configure('description'),
TextInput::make('brand'),
TextInput::make('sku'),
... $operation === Operation::Create->value ?
[FileUpload::make('files')->multiple()->disk('public')->directory('products')->visibility('public')] :
$entryArray,
Select::make('categories')
->multiple()
->relationship('categories', 'title')
->required(),
// ...
];
return $components;
});
return $schema
->components(function (?Model $record, string $operation) {
$entryArray = [];
if ($operation === Operation::Edit->value) {
$counter = 1;
foreach ($record->files as $file) {
$entryArray[] = ImageEntry::make('advertisement.file.id.' . $counter)->label('file ' . $counter)->state($file->url);
$counter++;
}
}
$components = [
TranslationComponent::configure('title')
->required(),
TranslationComponent::configure('description'),
TextInput::make('brand'),
TextInput::make('sku'),
... $operation === Operation::Create->value ?
[FileUpload::make('files')->multiple()->disk('public')->directory('products')->visibility('public')] :
$entryArray,
Select::make('categories')
->multiple()
->relationship('categories', 'title')
->required(),
// ...
];
return $components;
});
0 Replies