Custom Filament Table with Query of Left Joined Tables

I have managed to create livewire component with filament table in it as described here: https://filamentphp.com/docs/3.x/tables/adding-a-table-to-a-livewire-component In the example, the query for the table is given for a single model. (Product::query()) How can I pass the query for the filament table so that it will take 2 joined tables? SELECT * FROM employees LEFT JOIN attendances ON employees.id = attendances.employee_id
Solution:
Thanks, Dennis. I think I am getting somewhere. I tried as follow: ```public function table(Table $table): Table { return $table ->query(Employee::query()->leftJoin('attendances','employees.id','=','attendances.employee_id'))...
Jump to solution
11 Replies
krekas
krekas7mo ago
try using modifyQueryUsing method
setiawanh
setiawanhOP7mo ago
Can you help with an example for the above SQL query? I cannot seem to find the documentation for this method on Filament Table
krekas
krekas7mo ago
it's in different location
setiawanh
setiawanhOP7mo ago
What should I put in the function body?
krekas
krekas7mo ago
whatever you need to add to your query
setiawanh
setiawanhOP7mo ago
table ->query(Employee::query())->modifyQueryUsing(fn (Builder $query) => $query->leftJoin(...))? Something like this?
Dennis Koch
Dennis Koch7mo ago
Yes But when you provide ->query() already why don’t you put your query there?!
Solution
setiawanh
setiawanh7mo ago
Thanks, Dennis. I think I am getting somewhere. I tried as follow:
public function table(Table $table): Table
{
return $table
->query(Employee::query()->leftJoin('attendances','employees.id','=','attendances.employee_id'))
->columns([
TextColumn::make('first_name'), // column exists in Employee table and is showing
TextColumn::make('result'), // column exists in Attendances table but is NOT showing
])...
}
public function table(Table $table): Table
{
return $table
->query(Employee::query()->leftJoin('attendances','employees.id','=','attendances.employee_id'))
->columns([
TextColumn::make('first_name'), // column exists in Employee table and is showing
TextColumn::make('result'), // column exists in Attendances table but is NOT showing
])...
}
It seems to run correctly but the "result" column which supposes to get the data from the "attendances" table is not showing. Did I miss anything? Thanks!
Dennis Koch
Dennis Koch7mo ago
Check what query is actually run and what it returns for example with Laravel Debugbar
setiawanh
setiawanhOP7mo ago
Thanks! It is working now. There was a mistake in the "query" that I created.
Want results from more Discord servers?
Add your server