Laravel Filament Form show related data from other table

264 Views Asked by At

im currently develope my first project with filament. Im currently stuck at showing related data in a Resource => $form

In my case i need a form which show the salutation.name in the contact From. Im able to do this with the simple ViewPage (ViewContact.php) with $infolist but when i open the edit page the salutation.name is not shown.

Model/Crm/Contact

public function salutation(): BelongsTo
{
    return $this->belongsTo(Salutation::class, 'crm_salutation_id');
}

Model/Crm/Salutation

class Salutation extends Model {
    use HasFactory;

    protected $table = 'crm_salutations';
}

Filament/Resource/Crm/ContactResource/ContactResource.php

// Just for testing and check if salutation is shown without form
public static function infolist(Infolist $infolist): Infolist
{
    return $infolist
        ->schema([
        Section::make('')
            ->schema([
                Infolists\Components\TextEntry::make('company_name'),
                Infolists\Components\TextEntry::make('salutation.name'),
        ]);
}

public static function form(Form $form): Form
{
    return $form
        ->schema([
            Forms\Components\Section::make()
                ->schema([

                    Forms\Components\TextInput::make('company_id')
                        ->dehydrated(false),
                    //Forms\Components\TextInput::make('salutation.name'),

                    // just a test salutation still empty but i can search for it and worked fine to select it
                    Forms\Components\Select::make('salutation.name')
                        ->searchable()
                        ->getSearchResultsUsing(fn (string $query) => Salutation::where('name', 'like', "%{$query}%")->pluck('name'))
                        ->getOptionLabelUsing(fn ($value): ?string => Salutation::find($value)?->getAttribute('name')),

                    
        ]);
}

Im not sure but maybe i need to create a RelationManager for this?

1

There are 1 best solutions below

0
Tom On

Ok im to stuipid...super easy:

public static function form(Form $form): Form
{
return $form
    ->schema([
        Forms\Components\Section::make()
            ->schema([

                Forms\Components\TextInput::make('company_id')
                    ->dehydrated(false),
                Forms\Components\Select::make('crm_salutation_id')
                ->relationship('salutation', 'name'),

                
    ]);
}