F
Filament2y ago
Veur

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
Dennis Koch
Dennis Koch2y ago
Something like $client->users()->getQuery()?
Veur
VeurOP2y ago
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 filled
toeknee
toeknee2y ago
This occurs because you haven't defined the public property $active for livewire to post too. Following standard livewire processes
Veur
VeurOP2y ago
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?
toeknee
toeknee2y ago
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.
Veur
VeurOP2y ago
Ok. And do you have any thoughts on the getTableQuery() question (above)?
toeknee
toeknee2y ago
Not a clue on that one, sorry. I would did it on both. Suspect there might be a difference in the queries
wyChoong
wyChoong2y ago
Something like
protected function getTableQuery(): Builder
{
return User::query()->with(‘user’);
}
protected function getTableQuery(): Builder
{
return User::query()->with(‘user’);
}
Or maybe
protected function getTableQuery(): Builder
{
return User::query()->where(‘client_id’, $this->client->id);
}
protected function getTableQuery(): Builder
{
return User::query()->where(‘client_id’, $this->client->id);
}
toeknee
toeknee2y ago
Ahh of course, getQuery get's it relative the the params but user:query hasn't been built yet
Veur
VeurOP2y ago
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?
Want results from more Discord servers?
Add your server