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:
now the forms:
my model:
Only to give you guys more context about what I'm doing, a 'cargo' has many 'formulario' and a 'formulario' has many 'cargo'.
45 Replies
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
You need to pass your record to the form
$form->record($yourRecord)
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:
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
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:
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
I am not seeing a text input in the repeater? But if you added one... you would just do: $set('my_text_input', 'value')
I'm getting this error: Argument #1 ($record) must be of type App\Models\Cargo, null given
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
ok, did it and there was no error, but $record returned null in dd($record)
It will return null because no record exists?
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
public $record;
in mount:
$this->record = $record;
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
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...
ok, sorry
Solution
you can also do:
$form->model($this->record)
then
it worked, I'll try the rest by miself but if I have anymore problems I'll ask here
Unfortunetly I'm still not able to get my repeater to work in this custom page, I created a select for testing purpouses (I'm not going to use it) and it worked, so I have no ideia where the problem is
here's my current code:
my Cargo model:
my CargoFormulario model:
my Formulario model:
@toeknee can you help me?
Do not tag as per #✅┊rules
->relationship()
should be:
->relationship('cargoFormularios', 'cargo_id')
sorry i forgot about it, it returned this error: Filament\Forms\Components\Repeater::relationship(): Argument #2 ($modifyQueryUsing) must be of type ?Closure, string given
Do not tag as per ✅┊rulesIsn't it the same as answering to your previous message in that case? 😅
TextInput::make('id')It doesn't really make sense to edit the id in a repeater does it?
I just use it for testing
change to:
->relationship('cargoFormularios')
I want to get the 'formularios.pergunta'
there was no error but dd($record) returned null
sorry, I made a mistake, I want the formularios.pergunta to be shown in the label of text input
right so use
formularios
in the relationship and pergunta in the repeat textInput
it's a belongsToMany relationship, so the repeater has to have a relationship with the pivot, according to the documentation
Repeater requires a
HasMany
relation: https://filamentphp.com/docs/3.x/forms/fields/repeater#integrating-with-a-belongstomany-eloquent-relationshipyes. I'm using 'cargoFormularios' instead of 'formularios' because of that
Ah, sorry was confused because of the last messages. And
CargoFormulario
is a Pivot model?Yes it is
I lost track: What is the current issue you are facing? You no longer have the
called X on null
error, right?actualy the error is inside the textinput, the dd($record) returns null, but if I use dd in mount() it returns the correct array from Cargo , but i want to get the data inside Formulario, more specificly the 'pergunta' column
btw my repeater does not show anything to, like, i have to click the button for the textInput to show up
I think it's important to add again that I'm using a custom page inside CargoResource
the dd($record) returns nullMakes sense since
$record
refers to the Repeater record then and you haven't saved anything yet?
btw my repeater does not show anything to, like, i have to click the button for the textInput to show upDoes you model have any records linked?
if I comment the ->relationship it gets the data from Cargo, since it's public, you can see it in the 13 line here
but if the ->relationship is in the code, it shows nothing
not the CargoFormulario model, only Cargo model, but I have data in cargo_formulario table
Yes.
$record
refers to the current repeater record which is still null
I think you can get the original record via: $livewire->record
ok, but where should I use it? 😅 sorry, filament is actually my first backend language, so it's harder for me to solve some simple things
ok, but where should I use it?What's the goal? Something like this?
->label(fn ($livewire) => $livewire->record->id)
something like it, but from Formulario, like ->label(fn ($livewire) => $livewire->record->formularios.pergunta) or just ->pergunta
but for that I would need to have the data from formulario where formulario_id from CargoFormulario is = to id from Formulario, same for cargo_id and id from cargo
is it possible?
Sounds like what you actually want is a Select field inside the repeater:
Like the example in the docs:
https://filamentphp.com/docs/3.x/forms/fields/repeater#integrating-with-a-belongstomany-eloquent-relationship
it can't be, I want to make something like a quiz, 'pergunta' translated to english is 'question' and 'Formulario' is 'Quiz'
so the repeater will display the question in the TextInput's label, and the user will be able to answer and save in another table
Sorry, but it really sounds like this is a bit too much nesting inside the repeater
I think the same, I'll try to think about something diferent, thanks for your time and help