Inject Model in Livewire Component

I'm trying to inject my campaign model in a livewire component through form. I tried the instructions from https://filamentphp.com/docs/3.x/forms/adding-a-form-to-a-livewire-component#setting-a-form-model But i'm receiving this excepetion https://flareapp.io/share/Bm0YvpJP EditCampaign Class
protected static string $view = 'filament.resources.campaigns.pages.edit-campaign';

protected function getHeaderActions(): array
{
return [
Actions\ViewAction::make(),
Actions\DeleteAction::make(),
];
}
protected static string $view = 'filament.resources.campaigns.pages.edit-campaign';

protected function getHeaderActions(): array
{
return [
Actions\ViewAction::make(),
Actions\DeleteAction::make(),
];
}
filament.resources.campaigns.pages.edit-campaign blade view where i call my livewire component
<x-filament-panels::page>
<x-filament-panels::form wire:submit="save">
{{ $this->form }}

<x-filament-panels::form.actions :actions="$this->getCachedFormActions()" :full-width="$this->hasFullWidthFormActions()" />
</x-filament-panels::form>

@if (count($relationManagers = $this->getRelationManagers()))
<x-filament-panels::resources.relation-managers :active-manager="$activeRelationManager" :managers="$relationManagers" :owner-record="$record"
:page-class="static::class" />
@endif
<x-filament::section>
<x-slot name="heading">
Upload de mailing

</x-slot>
@livewire('upload-mailing')
</x-filament::section>
</x-filament-panels::page>
<x-filament-panels::page>
<x-filament-panels::form wire:submit="save">
{{ $this->form }}

<x-filament-panels::form.actions :actions="$this->getCachedFormActions()" :full-width="$this->hasFullWidthFormActions()" />
</x-filament-panels::form>

@if (count($relationManagers = $this->getRelationManagers()))
<x-filament-panels::resources.relation-managers :active-manager="$activeRelationManager" :managers="$relationManagers" :owner-record="$record"
:page-class="static::class" />
@endif
<x-filament::section>
<x-slot name="heading">
Upload de mailing

</x-slot>
@livewire('upload-mailing')
</x-filament::section>
</x-filament-panels::page>
Livewire Component Class
public Campaign $campaign;

public function mount(Campaign $campaign)
{
$this->form->fill($campaign->toArray());
}

public function form(Form $form)
{
return $form
->schema([
...
->statePath('data')
->model($this->campaign);
public Campaign $campaign;

public function mount(Campaign $campaign)
{
$this->form->fill($campaign->toArray());
}

public function form(Form $form)
{
return $form
->schema([
...
->statePath('data')
->model($this->campaign);
Thanks for any help.
Flare
Typed property App\Livewire\UploadMailing::$campaign must not be accessed before initialization - The error occurred at http://localhost/admin/campaigns/1/edit
3 Replies
Saade
Saade14mo ago
you don't have a property $campaign in your class, 1. Use property promotion public function mount(protected Campaign $campaign) 2. Or assign manually to your class
public function mount(Campaign $campaign)
{
$this->campaign = $campaign;
}
public function mount(Campaign $campaign)
{
$this->campaign = $campaign;
}
Victor Hugo Alves
Victor Hugo AlvesOP14mo ago
I have the property, first line of my example. I tried manually, but same problem. I "fixed" passing the record as a parameter in the livewire component.
public $campaign;

public function mount($campaign)
{
$this->campaign = $campaign;
$this->form->fill();
}
public $campaign;

public function mount($campaign)
{
$this->campaign = $campaign;
$this->form->fill();
}
<x-filament-panels::page>
<x-filament-panels::form wire:submit="save">
{{ $this->form }}

<x-filament-panels::form.actions :actions="$this->getCachedFormActions()" :full-width="$this->hasFullWidthFormActions()" />
</x-filament-panels::form>

@if (count($relationManagers = $this->getRelationManagers()))
<x-filament-panels::resources.relation-managers :active-manager="$activeRelationManager" :managers="$relationManagers" :owner-record="$record"
:page-class="static::class" />
@endif
<x-filament::section>
<x-slot name="heading">
Upload de mailing
</x-slot>
<livewire:upload-mailing :campaign="$record" />
</x-filament::section>
</x-filament-panels::page>
<x-filament-panels::page>
<x-filament-panels::form wire:submit="save">
{{ $this->form }}

<x-filament-panels::form.actions :actions="$this->getCachedFormActions()" :full-width="$this->hasFullWidthFormActions()" />
</x-filament-panels::form>

@if (count($relationManagers = $this->getRelationManagers()))
<x-filament-panels::resources.relation-managers :active-manager="$activeRelationManager" :managers="$relationManagers" :owner-record="$record"
:page-class="static::class" />
@endif
<x-filament::section>
<x-slot name="heading">
Upload de mailing
</x-slot>
<livewire:upload-mailing :campaign="$record" />
</x-filament::section>
</x-filament-panels::page>
Saade
Saade14mo ago
i'm sorry? the code you provided now is different from the one in the question
No description
Want results from more Discord servers?
Add your server