Set default value for inputs

I'm having trouble setting the default data for my form. Please help
No description
10 Replies
KingNii
KingNii3mo ago
I have tried // dd($company); $this->company = $company; // dd($this->company); $this->form->fill([ 'company_name' => $company->company_name, ]);
awcodes
awcodes3mo 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
KingNii3mo ago
@awcodes I see, I’m trying to have users edit their information with this code.
LeandroFerreira
LeandroFerreira3mo ago
try to share the whole code that you are using (code not image please)
KingNii
KingNii3mo 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
KingNii3mo 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
LeandroFerreira3mo 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
KingNii3mo ago
@Leandro Ferreira Can I add the image to the fileupload field??
LeandroFerreira
LeandroFerreira3mo ago
Yes
KingNii
KingNii3mo 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
More Posts
Using Related Field Data in Validation MessagesCan anyone tell me how to pass related data into the Validation message? Currently, this works as I How to ignore the mutator and get the original value of a property?I have a user model that has a getEmailAttribute that perform mutations to the user emails, how can generating a unique id for each builder blockI'm a little stuck on this, trying to have a hidden field (ideally) that generates a UUID, which is How to Warn Users of Unsaved Changes on the TenantProfile Page?Hi Guys, I have a page "EditCompanyProfile" which extends `EditTenantProfile`. Editing and saving woMultiple select component data is returning null on a livewire form componentI have a livewire component with an integration with Form Builder, with this code: `public ?array Header action take 30s to respondIm not sure what happened, but livewire takes 15-30s to respond after clicking header actionUpdate parent record with SUM of two related tablesI need to update the parent record with the sum of the related table and wondering what would be theError Filament has not been installed yet: php artisan filament:install --panelsHello Goodmorning when I am using this command ``composer require filament/filament:"^3.2" -W`` it gBug with sidebar notifications not marked as read but deletedWhen you dismiss a sidebar notification they are deleted rather than marked as read. Not sure this Keep the database notification on closeWhat I'm trying to do: - I want to keep database notifications in databases after users close the no