asifrumani's avatar

Pre-populate parent ID in select in RelationManager when creating record

I am not very well versed with Filament, just the basic stuff. I am using Filament v4. I have models Customer and Deal. Customer model has HasMany relationship with Deals and Deal model has BelongsTo relationship with Customer. I have DealRelationManager in CustomerResource which lists all Deals for a customer when in View or Edit mode. I am trying to work out a way to automatically populate the 'customer_id' Select with appropriate customer_id when creating a new Deal record through the DealRelationManager.

Any suggestions are appreciated. Thank you in advance.

0 likes
2 replies
LaryAI's avatar
Level 58

Sure! When using Filament v4's RelationManagers (such as DealRelationManager within your CustomerResource), Filament automatically manages the relationship for you. Specifically, when creating a new Deal via the RelationManager, you don't need to manually select the customer_id—it should be assigned under the hood by Filament.

However, if you want to show the field (for reference) but keep it read-only/pre-filled, or hide it entirely, here's how you can approach it:

1. You Don't Need to Show customer_id In the Create Form

Normally, Filament will automatically associate the new Deal with the parent Customer, so you can simply remove the customer_id Select field from your Deal form schema inside the RelationManager. (That's the recommended way.)

2. If You Want to Show It as a Disabled/Read-Only Field

If you want to show the field for informational purposes (e.g. disabled or read-only), you can use the disabled() or readOnly() methods, and prefill the value with the parent record's ID.

Example:

// In your DealRelationManager

public static function form(Form $form): Form
{
    return $form
        ->schema([
            // Other Deal fields...

            // Show customer_id as disabled (optional)
            Select::make('customer_id')
                ->label('Customer')
                ->options(Customer::all()->pluck('name', 'id'))
                ->default(fn ($livewire) => $livewire->getOwnerRecord()->id)
                ->disabled()
                ->required(),
        ]);
}
  • default(fn ($livewire) => $livewire->getOwnerRecord()->id) sets the value to the parent Customer ID.
  • .disabled() will make the field non-editable.

3. Completely Hide the customer_id Field

You can also exclude the customer_id field from the form in your RelationManager. Filament will link the new Deal to its parent Customer as expected.

// Just don't include a customer_id field in your schema inside DealRelationManager
public static function form(Form $form): Form
{
    return $form
        ->schema([
            // Only add the other Deal fields (not customer_id)
        ]);
}

Recap

  • Filament sets the foreign key automatically inside RelationManager.
  • Don’t add customer_id to the form unless you have a specific need.
  • If you must display it, set it as disabled/read-only with the default from the owner record.

That's all you need! If it doesn't work as expected, make sure your relationships (hasMany, belongsTo) are set up correctly in your models.

Please or to participate in this conversation.