Table Widget - query with join
Hello,
I have simple Table widget with custom query that use JOIN.
I can't use relations but need to use JOIN function
How can I call join fields in columns?
TextColumn::make('activity_group.activity_id') in code below
Thanks
<?php
namespace App\Filament\Widgets;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Tables\Columns\TextColumn;
use Filament\Widgets\TableWidget as BaseWidget;
use Filament\Widgets\Concerns\InteractsWithPageFilters;
use App\Models\UserActivity;
class GroupsReportTable extends BaseWidget
{
use InteractsWithPageFilters;
public function table(Table $table): Table
{
$filtro = $this->filters['filtro'] ?? 0;
return $table
->query(UserActivity::query()
->leftJoin("activity_group", "user_activities.activity_id" ,"=", "activity_group.activity_id")
)
->columns([
TextColumn::make('id'),
TextColumn::make('activity_group.activity_id'),
]);
}
}
<?php
namespace App\Filament\Widgets;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Tables\Columns\TextColumn;
use Filament\Widgets\TableWidget as BaseWidget;
use Filament\Widgets\Concerns\InteractsWithPageFilters;
use App\Models\UserActivity;
class GroupsReportTable extends BaseWidget
{
use InteractsWithPageFilters;
public function table(Table $table): Table
{
$filtro = $this->filters['filtro'] ?? 0;
return $table
->query(UserActivity::query()
->leftJoin("activity_group", "user_activities.activity_id" ,"=", "activity_group.activity_id")
)
->columns([
TextColumn::make('id'),
TextColumn::make('activity_group.activity_id'),
]);
}
}
Solution:Jump to solution
Solved using select()
```php
public function table(Table $table): Table
{
...
1 Reply
Solution
Solved using select()
public function table(Table $table): Table
{
$filtro = $this->filters['filtro'] ?? 0;
return $table
->query(UserActivity::query()
->join("activity_group", "user_activities.activity_id" ,"=", "activity_group.activity_id")
->join("groups", "groups.id" ,"=", "activity_group.group_id")
->select("user_activities.*", "groups.descrizione AS groups_descrizione")
)
->columns([
TextColumn::make('id'),
TextColumn::make('groups_descrizione'),
]);
}
public function table(Table $table): Table
{
$filtro = $this->filters['filtro'] ?? 0;
return $table
->query(UserActivity::query()
->join("activity_group", "user_activities.activity_id" ,"=", "activity_group.activity_id")
->join("groups", "groups.id" ,"=", "activity_group.group_id")
->select("user_activities.*", "groups.descrizione AS groups_descrizione")
)
->columns([
TextColumn::make('id'),
TextColumn::make('groups_descrizione'),
]);
}