In relationshipManager, the relationship is not automatically set to ownerRecord

In these screenshot, you can see I'm in the Provider model, in the OpenAI Provider. When I click the "Create" in the Relationship Manager to create a new Model, the Provider is not automatically set to "Open AI".
No description
No description
11 Replies
mrleblanc101
mrleblanc101OP4mo ago
I'd like it to prefill "Open AI" as the provider, since I opened the modal from this relationship. Maybe it could even be disabled
No description
mrleblanc101
mrleblanc101OP4mo ago
Here is the relationshipManager code:
class AiModelsRelationManager extends RelationManager
{
protected static string $relationship = 'aiModels';

protected static ?string $title = 'Modèles';

public function form(Form $form): Form
{
return AiModelResource::form($form);
}

public function table(Table $table): Table
{
return AiModelResource::table($table)
->recordTitleAttribute('prompt')
->filters([
//
])
->headerActions([
// TODO: Create action doesn't default to current relationship
Tables\Actions\CreateAction::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
}
class AiModelsRelationManager extends RelationManager
{
protected static string $relationship = 'aiModels';

protected static ?string $title = 'Modèles';

public function form(Form $form): Form
{
return AiModelResource::form($form);
}

public function table(Table $table): Table
{
return AiModelResource::table($table)
->recordTitleAttribute('prompt')
->filters([
//
])
->headerActions([
// TODO: Create action doesn't default to current relationship
Tables\Actions\CreateAction::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
}
And the form in the Resource:
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->label('Nom')
->columnSpan('full')
->required(),
Forms\Components\Select::make('ai_provider_id')
->label('Développeur')
->searchable()
->columnSpan('full')
->relationship('aiProvider', 'name')
->createOptionForm([
Forms\Components\TextInput::make('name')
->label('Nom')
->required(),
])
->preload(),
]);
}
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->label('Nom')
->columnSpan('full')
->required(),
Forms\Components\Select::make('ai_provider_id')
->label('Développeur')
->searchable()
->columnSpan('full')
->relationship('aiProvider', 'name')
->createOptionForm([
Forms\Components\TextInput::make('name')
->label('Nom')
->required(),
])
->preload(),
]);
}
Wait ! Am I supposed to omit the relationship field when I'm creating via the relationship manager ? I'm not sure how I would do that and still reuse my resource form !
Dan Harrin
Dan Harrin4mo ago
yeah you dont need the relationship field we automatically associate it
mrleblanc101
mrleblanc101OP4mo ago
If I reuse the form, how do I hide this field only in the relationship manager ?
Dan Harrin
Dan Harrin4mo ago
you can do a hiddenOn(RelationManagerClassHere::class) I think
mrleblanc101
mrleblanc101OP4mo ago
I'll try that, thanks ! @Dan Harrin I see your filamentphp badge, do you think it would be a good idea to mention this in the Code quality tips ? Someone pointed me to that page in this thread to reuse schema because I wanted to avoid field repetition between Form, Relationship Manager and createOptionForm. Or should relationship automatically be hidden when they conrespond to the currently displayed relationship manager ? @Dan Harrin Ohh, I see you're co-creator, nice ♥️ If you think this is a good idea, I can open this as a Github Issue / Discussion to improve the docs
KarL
KarL4mo ago
Forms\Components\Select::make('ai_provider_id') ->label('Développeur') ->searchable() ->columnSpan('full') ->relationship('aiProvider', 'name') ->createOptionForm([ Forms\Components\TextInput::make('name') ->label('Nom') ->required(), ]) ->preload() ->default(fn ($livewire) => $livewire instanceof \Filament\Resources\RelationManagers\RelationManager ? $livewire->getOwnerRecord()->getKey() : null),
Dan Harrin
Dan Harrin4mo ago
theres no need to do this though, Filament automatically associates the correct foreign key you can remove the component completely the docs are open source so you can submit a PR to the repo for them 👍
mrleblanc101
mrleblanc101OP4mo ago
Good, I'll open a PR to improve the Code quality tips page in that case. Hopefully it gets merged 👌
Dan Harrin
Dan Harrin4mo ago
its also in the v3 docs

Did you find this page helpful?