Repeater relationship returns null

So, I have an issue where my relationship can't be found by repeater in my custom page, I want it to display every 'pergunta' inside the table formulario but I can't find the solution , this is the error returned: Call to a member function cargoFormulario() on null here's the code:
class Perguntas extends Page implements HasForms
{
use InteractsWithForms;

protected static ?string $model = Pergunta::class;
class Perguntas extends Page implements HasForms
{
use InteractsWithForms;

protected static ?string $model = Pergunta::class;
now the forms:
return $form
->schema([
Repeater::make('cargoFormulario')

->relationship()
->schema([
TextInput::make('texto')
])
]);
return $form
->schema([
Repeater::make('cargoFormulario')

->relationship()
->schema([
TextInput::make('texto')
])
]);
my model:
class Pergunta extends Model
{
protected $table = 'formularios';
protected $guarded = ['id'];
use HasFactory;

public function cargoFormulario()
{
$this->hasMany(CargoFormulario::class, 'id', 'formulario_id');
}
}
class Pergunta extends Model
{
protected $table = 'formularios';
protected $guarded = ['id'];
use HasFactory;

public function cargoFormulario()
{
$this->hasMany(CargoFormulario::class, 'id', 'formulario_id');
}
}
Only to give you guys more context about what I'm doing, a 'cargo' has many 'formulario' and a 'formulario' has many 'cargo'.
Solution:
you can also do: $form->model($this->record) then...
Jump to solution
45 Replies
hosmar27
hosmar272mo ago
The custom page doesn't have a resource and I created that model ('Pergunta') after creating this page, its basically the name of the page I have models for 'Formulario', 'Cargo' and 'CargoFormulario' , if needed I can post it here
Dennis Koch
Dennis Koch2mo ago
You need to pass your record to the form $form->record($yourRecord)
hosmar27
hosmar272mo ago
couldn't solve it, it returns this error 'Undefined variable $record' I've created a new custom page on a resource my code is able to pass the id in the url, but I can't get any data on form, this is how I did:
->url(fn (Cargo $record): string => route('filament.admin.resources.cargos.form', ['record' => $record]))
->url(fn (Cargo $record): string => route('filament.admin.resources.cargos.form', ['record' => $record]))
toeknee
toeknee2mo ago
That is because it is a create repeater record doesn't exist, I think you want the parent record so $livewire then $livewire->owner_record
hosmar27
hosmar272mo ago
I'll try it I did some progress and could get $record filled with the selected array that I wanted, now I just need to fill the forms with it i've tried to use this in many ways but couldn't do it here's my current code:
class FormCargo extends Resource implements HasForms
{
use InteractsWithForms;
use InteractsWithRecord;

protected static string $resource = CargoResource::class;

protected static string $view = 'filament.resources.cargo-resource.pages.form-cargo';

public ?array $data = [];

public function mount(Cargo $record): void
{
dd($record);
// $this->form = $record;
}

public static function form(Form $form): Form
{
return $form
->schema([
Repeater::make('formularios')
->schema([
TextInput::make('id')
->label(function (Cargo $record): array
{
dd($record);
})
])
->statePath('data')
]);
}

public function fill($record)
{
// dd($record);
// return $record;
}
class FormCargo extends Resource implements HasForms
{
use InteractsWithForms;
use InteractsWithRecord;

protected static string $resource = CargoResource::class;

protected static string $view = 'filament.resources.cargo-resource.pages.form-cargo';

public ?array $data = [];

public function mount(Cargo $record): void
{
dd($record);
// $this->form = $record;
}

public static function form(Form $form): Form
{
return $form
->schema([
Repeater::make('formularios')
->schema([
TextInput::make('id')
->label(function (Cargo $record): array
{
dd($record);
})
])
->statePath('data')
]);
}

public function fill($record)
{
// dd($record);
// return $record;
}
the 'dd($record)' in function mount returns the correct array, just need to find a way to send the description to the TextInput label inside the Repeater I want to make a quiz by the way
toeknee
toeknee2mo ago
I am not seeing a text input in the repeater? But if you added one... you would just do: $set('my_text_input', 'value')
hosmar27
hosmar272mo ago
No description
hosmar27
hosmar272mo ago
I'm getting this error: Argument #1 ($record) must be of type App\Models\Cargo, null given
->schema([
Repeater::make('formularios')
->schema([
TextInput::make('id')
->label(function (Cargo $record): ?array
{
dd($record);
})
])
])
->statePath('data');
->schema([
Repeater::make('formularios')
->schema([
TextInput::make('id')
->label(function (Cargo $record): ?array
{
dd($record);
})
])
])
->statePath('data');
toeknee
toeknee2mo ago
You will if record is null.... you specificed you expect Cargo so remove Cargo from it and then you can check if it's null
hosmar27
hosmar272mo ago
ok, did it and there was no error, but $record returned null in dd($record)
toeknee
toeknee2mo ago
It will return null because no record exists?
hosmar27
hosmar272mo ago
yes, but I'm able to get the $record in function mount, I don't know how to send it to form here, the dd returns the correct $record
toeknee
toeknee2mo ago
public $record; in mount: $this->record = $record;
hosmar27
hosmar272mo ago
I think I did not explain it really well, I'm already receiving $record in mount, but I am not receiving it in my form
toeknee
toeknee2mo ago
You are receiving it, but you are not setting it for your form to use it. I have told you above how to set it...
Want results from more Discord servers?
Add your server