Set default value for inputs

I'm having trouble setting the default data for my form. Please help
No description
10 Replies
KingNii
KingNiiOP8mo ago
I have tried // dd($company); $this->company = $company; // dd($this->company); $this->form->fill([ 'company_name' => $company->company_name, ]);
awcodes
awcodes8mo ago
$this in your context is the livewire component or the field, not the model. But also you can’t use a relationship for default because the model doesn’t exist in the create content. If you want a default you will need to do a manual query against the company table to get a default record for the company relationship. Also be aware that default() on works in the context of creating a record.
KingNii
KingNiiOP8mo ago
@awcodes I see, I’m trying to have users edit their information with this code.
LeandroFerreira
LeandroFerreira8mo ago
try to share the whole code that you are using (code not image please)
KingNii
KingNiiOP8mo ago
// This is the Company Settings component
public function render()
{
return view('livewire.v1.admin.company.company-settings', [
'company' => Company::where('user_id', auth()->user()->id)->first(),
// 'company' => auth()->user()->company
]);
}

// This is the blade component
<div class="flex-1">
<livewire:v1.admin.company.company-edit :company="$company" />
</div>

//This is the edit component
public function mount(Company $company)
{
// dd($company);
// $this->company = Company::where('user_id', auth()->user()->company->id)->first();
$this->company = $company;
// dd($this->company);
$this->form->fill([
'company_name' => $company->company_name,
]);
}

Tabs\Tab::make('Company Details')
->schema([
// ...
TextInput::make('company_name')
->label('Company Name')
->default(fn (Set $set, ?string $state) => $set('company_name', $this->company->company_name))
// ->default(fn (Set $set, ?string $state) => $set('company_name', $this->company->company_name))
// ->afterStateHydrated(function (TextInput $component, string $state) {
// $component->state($this->company->company_name);
// // $component->state(ucwords($state));
// })
->required()
->maxLength(255),

// This is the Company Settings component
public function render()
{
return view('livewire.v1.admin.company.company-settings', [
'company' => Company::where('user_id', auth()->user()->id)->first(),
// 'company' => auth()->user()->company
]);
}

// This is the blade component
<div class="flex-1">
<livewire:v1.admin.company.company-edit :company="$company" />
</div>

//This is the edit component
public function mount(Company $company)
{
// dd($company);
// $this->company = Company::where('user_id', auth()->user()->company->id)->first();
$this->company = $company;
// dd($this->company);
$this->form->fill([
'company_name' => $company->company_name,
]);
}

Tabs\Tab::make('Company Details')
->schema([
// ...
TextInput::make('company_name')
->label('Company Name')
->default(fn (Set $set, ?string $state) => $set('company_name', $this->company->company_name))
// ->default(fn (Set $set, ?string $state) => $set('company_name', $this->company->company_name))
// ->afterStateHydrated(function (TextInput $component, string $state) {
// $component->state($this->company->company_name);
// // $component->state(ucwords($state));
// })
->required()
->maxLength(255),

@Leandro Ferreira This is a sample
KingNii
KingNiiOP8mo ago
This is what I get when I dump the $company in
public function mount(Company $company)
{
dd($company);
// $this->company = Company::where('user_id', auth()->user()->company->id)->first();
$this->company = $company;
// dd($this->company);
$this->form->fill([
'company_name' => $company->company_name,
]);
}
public function mount(Company $company)
{
dd($company);
// $this->company = Company::where('user_id', auth()->user()->company->id)->first();
$this->company = $company;
// dd($this->company);
$this->form->fill([
'company_name' => $company->company_name,
]);
}
No description
LeandroFerreira
LeandroFerreira8mo ago
Where is your $form? Are you using a statePath? Check this section: https://filamentphp.com/docs/3.x/forms/adding-a-form-to-a-livewire-component#adding-the-form If you are filling the company_name in the mount method, you don't need ->default(xx)
KingNii
KingNiiOP8mo ago
@Leandro Ferreira Can I add the image to the fileupload field??
LeandroFerreira
LeandroFerreira8mo ago
Yes
KingNii
KingNiiOP8mo ago

This is what I did and it worked but can I get the image have a default value

$this->form->fill([
'company_name' => $company->company_name,
'postal' => $company->postal,
'company_phone' => $company->company_phone,
'company_email' => $company->company_email,
'company_logo' => $company->company_logo,
'address' => $company->address,
'website' => $company->website,
'services' => $company->services,
'images' => $company->image,
]);

This is what I did and it worked but can I get the image have a default value

$this->form->fill([
'company_name' => $company->company_name,
'postal' => $company->postal,
'company_phone' => $company->company_phone,
'company_email' => $company->company_email,
'company_logo' => $company->company_logo,
'address' => $company->address,
'website' => $company->website,
'services' => $company->services,
'images' => $company->image,
]);
@Leandro Ferreira this is what I did
Want results from more Discord servers?
Add your server