Return a BelongsToMany in getTableQuery()
I have a Livewire component with a
Client $client
model property. In that component I'd like to render a table with the users of that client, which I can access with $client->users()
(BelongsToMany)
Is there a way to use $client->users()
inside getTableQuery()
, instead of writing the Eloquent query?10 Replies
Something like
$client->users()->getQuery()
?Of course, thanks 😅
When I use the relationship inside
getTableQuery()
, the record actions aren't working anymore. For example this action...
Action::make('status')
->mountUsing(fn (ComponentContainer $form, $record) => $form->fill([
'active' => $record->active,
]))
->action(function ($record, array $data): void {
$record->update($data);
})
->form([
Toggle::make('active'),
]),
...works when I return User::query()
in getTableQuery()
.
But when returning $client->users()->getQuery()
it shows this error:
Livewire Entangle Error: Livewire property 'mountedTableActionData.active' cannot be found
I checked the $record
and it contains the same data in both situations. It looks like mountedTableActionData
is not filledThis occurs because you haven't defined the public property $active for livewire to post too. Following standard livewire processes
But why does the exact same Action work when I have this getTableQuery:
protected function getTableQuery(): Builder
{
return User::query();
}
And not with this:
protected function getTableQuery(): Builder
{
return $this->client->users()->getQuery();
}
Also, active
is an attribute on the $record
, so it doesn't make sense that it should be a public property, right?If you understand livewire, when you submit a form it gets all the data then passes it into the public properties of the component, you are pasting it in but the fields are not declards.
You probably want to look at setting a public property of say $form and then within the form statePath('form') so all fields under it will be assigned to form or similar.
Ok. And do you have any thoughts on the
getTableQuery()
question (above)?Not a clue on that one, sorry. I would did it on both. Suspect there might be a difference in the queries
Something like
Or maybe
Ahh of course, getQuery get's it relative the the params but user:query hasn't been built yet
I see. So the answer to my original question is: it's not possible. I have to write the query instead of calling the relationship?