user2950020
user2950020

Reputation: 67

Laravel Filament Custom page with Repeater

I'm having trouble populating and subsequently saving the relationship data I have in my User.php (UserProfessionalExperience) model.

This error is printed on the screen:

Call to a member function UserProfessionalExperience() on null

I thought that if I rescued auth()->user() I would be able to use relationship() in Repeater but strangely it doesn't bring the relationship. And in mount() if I run dd(auth()->user()->UserProfessionalExperience) it correctly brings the data

This is my code:

<?php

namespace App\Filament\Candidato\Pages;

use Filament\Pages\Page;
use Filament\Support\Exceptions\Halt;
use Filament\Notifications\Notification;
use Filament\Actions\Action;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Form;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Card;
use Filament\Forms\Components\Grid;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Repeater;
use App\Models\UserProfessionalExperience;
use App\Models\User;

class EditProfessionalExperience extends Page implements HasForms
{
    use InteractsWithForms;

    public ?array $data = []; 

    protected static ?string $navigationIcon = 'heroicon-o-document-text';

    protected static string $view = 'filament.candidato.pages.edit-professional-experience';

    protected static ?string $title = 'Experiência Profissional';
 
    protected static ?string $navigationLabel = 'Experiência Profissional';
    
    protected static ?string $slug = 'experiencia-profissional';

    public function mount(): void 
    {
        $this->form->fill(auth()->user()->attributesToArray()); 
    }
    public function form(Form $form): Form
    {
        return $form
            ->schema([
                Section::make('Experiência Profissional')
                ->description('Nesta seção, você pode listar suas experiências profissionais passadas. Isso inclui cargos, empresas e suas principais responsabilidades. Adicione quantas experiências quiser.')
                ->schema([
                    Repeater::make('UserProfessionalExperience')->relationship()->columns(2)->schema([
                        TextInput::make('company_name')
                            ->label('Nome da empresa')
                            ->required(),
                    ])->mutateRelationshipDataBeforeFillUsing(function (array $data): array {
                        $data['user_id'] = auth()->id();
                 
                        return $data;
                    })
                ])
            ])->statePath('data');
    }

    protected function getFormActions(): array
    {
        return [
            Action::make('save')
                ->label(__('filament-panels::resources/pages/edit-record.form.actions.save.label'))
                ->submit('save'),
        ];
    }
    public function save(): void
    {
        try {
            $data = $this->form->getState();
 
            auth()->user()->UserProfessionalExperience->update($data);
        } catch (Halt $exception) {
            return;
        }

        Notification::make() 
            ->success()
            ->title(__('filament-panels::resources/pages/edit-record.notifications.saved.title'))
            ->send();
    }
}

My model User:

public function UserProfessionalExperience()
    {
        return $this->hasMany(\App\Models\UserProfessionalExperience::class, 'user_id');
    }

Upvotes: 0

Views: 2475

Answers (1)

KennBey
KennBey

Reputation: 1

I might be late but i got the same problem before and in my case i solved it by doing this :

public ?array $data = [];

public ?User $record = null;

public function mount(): void
{
    $this->record = auth()->user();
    $this->fillForm();
}

public function fillForm(): void
{
    $data = $this->record->attributesToArray();

    $data = $this->mutateFormDataBeforeFill($data);

    $this->form->fill($data);
}

public function mutateFormDataBeforeFill(array $data): array
{
    // STORE TEAMS
    $data['teams'] = $this->record->teams()->get()->toArray();

    return $data;
}

As you saw, my relation is 'teams', i saved it in my data array by using a User record, then i just wrote at the end of my form :

        ->model($this->record)
        ->statePath('data');

In my case that solved it, hope it works for you too.

Upvotes: 0

Related Questions