F
Filament5mo ago
hannes

auto selecting company_id

Hello friends, please tell me how to make the attached screenshot so that instead of company_id, the current company is selected automotically (I have a multi tenancy implementation) Form code:
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Select::make('company_id')
->relationship('company', 'name')
->required(),
Forms\Components\TextInput::make('address')
->required()
->suffixIcon('heroicon-m-globe-alt')
->maxLength(255),
Forms\Components\TextInput::make('email')
->required()
->email()
->suffixIcon('heroicon-o-envelope')
->maxLength(255),
Forms\Components\TextInput::make('number')
->suffixIcon('heroicon-m-device-phone-mobile')
->required()
->numeric(),
]);
}
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Select::make('company_id')
->relationship('company', 'name')
->required(),
Forms\Components\TextInput::make('address')
->required()
->suffixIcon('heroicon-m-globe-alt')
->maxLength(255),
Forms\Components\TextInput::make('email')
->required()
->email()
->suffixIcon('heroicon-o-envelope')
->maxLength(255),
Forms\Components\TextInput::make('number')
->suffixIcon('heroicon-m-device-phone-mobile')
->required()
->numeric(),
]);
}
No description
5 Replies
Andrew
Andrew5mo ago
I'm just taking a stab at this. Does your model define the relationship between company contacts and the company? On your CompanyContacts Model: public function company(): BelongsTo { return $this->belongsTo(Company::class); } then I think you should be able to do: Forms\Components\Select::make('company.name') and it will use the relationship ?
hannes
hannes5mo ago
I've already done half the work, now I need the company name to be shown in the Company selection Field, not a void. 1 screenshot of the photo, as it should be. 2 screenshot like mine
No description
No description
hannes
hannes5mo ago
Code:
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Select::make('company_id')
->reactive()
->options(\App\Models\Company::where('id', session()->get('company_id'))->pluck('name', 'id'))
->default(session()->get('company_id'))
->disabled(),
Forms\Components\TextInput::make('inn')
->required()
->numeric(),
Forms\Components\TextInput::make('kpp')
->required()
->numeric(),
Forms\Components\TextInput::make('ogrn')
->required()
->numeric(),
]);
}
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Select::make('company_id')
->reactive()
->options(\App\Models\Company::where('id', session()->get('company_id'))->pluck('name', 'id'))
->default(session()->get('company_id'))
->disabled(),
Forms\Components\TextInput::make('inn')
->required()
->numeric(),
Forms\Components\TextInput::make('kpp')
->required()
->numeric(),
Forms\Components\TextInput::make('ogrn')
->required()
->numeric(),
]);
}
Arnaud
Arnaud5mo ago
Hello To get the current company, you can do Filament::getTenant(), if company is your tenant of course If it's always the current company, you can remove this field and mutate before save/create to add company id like $data['company_id'] = Filament::getTenant()->id
Sjoerd24
Sjoerd245mo ago
plus if you are working with tenants, the company_id will automaticlly be set. you can maybe just remove it all.. (if your model Company has a hasmany relation with contacts that is)