How to set prependActions on table

Is there a similar method to the ->prependActions() method when creating a table definition in a TableWidget? I'm getting the Table Actions like this:
protected function getTableActions(): array
{
return [
Impersonate::make('impersonate')->icon('heroicon-o-users'),
];
}
protected function getTableActions(): array
{
return [
Impersonate::make('impersonate')->icon('heroicon-o-users'),
];
}
but I don't think there's a getTablePrependActions() method similar to ->prependActions() , so I tried making a public function, but that didn't work.
public function getTablePrependActions(): int | string | array
{
return [
Impersonate::make('impersonate'),
];
}

public function render(): View
{
$this->getTablePrependActions();
return view('filament.widgets.user-widget');
}
public function getTablePrependActions(): int | string | array
{
return [
Impersonate::make('impersonate'),
];
}

public function render(): View
{
$this->getTablePrependActions();
return view('filament.widgets.user-widget');
}
Where am I going wrong here?
17 Replies
Dennis Koch
Dennis Koch2y ago
I think it was deprecated on the table. Why do you need prependAction()? Just add all actions you want into getTableActions()
usmcgator
usmcgatorOP2y ago
it seems to get the Impersonate action/icon to show, I need to set both the table actions and table prepend actions. I tried creating the table definition the other way, but it wasn't compatible with TableWidget it works fine in my user resource, but I'm trying to create a widget showing similar results that also includes the impersonate action.
Dennis Koch
Dennis Koch2y ago
What is not working with just using getTableActions()?
usmcgator
usmcgatorOP2y ago
the impersonate icon is not showing up, which I'm assuming is because I'm not setting it in prepend actions I have this working on the user resource page, but it doesn't show up on the user widget I created. the main difference is I'm creating the table definition in the user resource using the method where I can set the prependAction. It appears you can't create tables the same way in tablewidgets, so I can't set that value.
Dennis Koch
Dennis Koch2y ago
There is not prependActions() and this feature just merged to actions into the same array, so there should be no difference. Let me just try this with an installation I have
Dennis Koch
Dennis Koch2y ago
Works perfectly fine.
Dennis Koch
Dennis Koch2y ago
protected function getTableActions(): array
{
return [
Impersonate::make('impersonate'),
];
}
protected function getTableActions(): array
{
return [
Impersonate::make('impersonate'),
];
}
No sure whether that affects actions, but do you have any permissions/policies that might hide this? Can you show the full table widget code?
usmcgator
usmcgatorOP2y ago
<?php

namespace App\Filament\Resources\UserResource\Widgets;

use Closure;
use App\Models\User;
use Filament\Widgets\TableWidget;
use Illuminate\Contracts\View\View;
use Filament\Tables\Columns\TextColumn;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Contracts\Support\Htmlable;
use STS\FilamentImpersonate\Tables\Actions\Impersonate;

class UserWidget extends TableWidget
{
protected static string $view = 'filament.resources.user-resource.widgets.user-widget';

protected function getTableHeading(): string|Htmlable|Closure|null
{
return "Users";
}

protected function getTableQuery(): Builder
{
return User::query()
->where('status', '=', 'active');
}

protected function getTableColumns(): array
{
return [
TextColumn::make('name')->label('Name'),
TextColumn::make('email')->label('Email'),
];
}

protected function getTableActions(): array
{
return [
Impersonate::make('impersonate'),
];
}

public function isTableSearchable(): bool
{
return false;
}

protected function getTableRecordsPerPageSelectOptions(): array
{
return [10, 25, 50];
}

public function render(): View
{
return view('filament.resources.user-resource.widgets.user-widget');
}
}
<?php

namespace App\Filament\Resources\UserResource\Widgets;

use Closure;
use App\Models\User;
use Filament\Widgets\TableWidget;
use Illuminate\Contracts\View\View;
use Filament\Tables\Columns\TextColumn;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Contracts\Support\Htmlable;
use STS\FilamentImpersonate\Tables\Actions\Impersonate;

class UserWidget extends TableWidget
{
protected static string $view = 'filament.resources.user-resource.widgets.user-widget';

protected function getTableHeading(): string|Htmlable|Closure|null
{
return "Users";
}

protected function getTableQuery(): Builder
{
return User::query()
->where('status', '=', 'active');
}

protected function getTableColumns(): array
{
return [
TextColumn::make('name')->label('Name'),
TextColumn::make('email')->label('Email'),
];
}

protected function getTableActions(): array
{
return [
Impersonate::make('impersonate'),
];
}

public function isTableSearchable(): bool
{
return false;
}

protected function getTableRecordsPerPageSelectOptions(): array
{
return [10, 25, 50];
}

public function render(): View
{
return view('filament.resources.user-resource.widgets.user-widget');
}
}
Dennis Koch
Dennis Koch2y ago
Can you try removing $view and $render?
usmcgator
usmcgatorOP2y ago
I removed $view and the render method and it had no impact still shows a table of users, just no impersonate icon
Dennis Koch
Dennis Koch2y ago
Weird. I am out of ideas. Sorry.
usmcgator
usmcgatorOP2y ago
can you share your user widget code that works?
Dennis Koch
Dennis Koch2y ago
<?php

namespace App\Filament\Resources\UserResource\Widgets;

use App\Models\User;
use Filament\Tables;
use Filament\Widgets\TableWidget;
use Illuminate\Database\Eloquent\Builder;
use STS\FilamentImpersonate\Impersonate;

class UserWidget extends TableWidget
{
protected function getTableQuery(): Builder
{
return User::query();
}

protected function getTableColumns(): array
{
return [
Tables\Columns\TextColumn::make('name')->label('Name'),

Tables\Columns\TextColumn::make('email')->label('E-Mail'),
];
}

protected function getTableActions(): array
{
return [
Impersonate::make('impersonate'),
];
}
}
<?php

namespace App\Filament\Resources\UserResource\Widgets;

use App\Models\User;
use Filament\Tables;
use Filament\Widgets\TableWidget;
use Illuminate\Database\Eloquent\Builder;
use STS\FilamentImpersonate\Impersonate;

class UserWidget extends TableWidget
{
protected function getTableQuery(): Builder
{
return User::query();
}

protected function getTableColumns(): array
{
return [
Tables\Columns\TextColumn::make('name')->label('Name'),

Tables\Columns\TextColumn::make('email')->label('E-Mail'),
];
}

protected function getTableActions(): array
{
return [
Impersonate::make('impersonate'),
];
}
}
Can the shown users even be impersonated?
usmcgator
usmcgatorOP2y ago
hot digity, that worked. Not sure what I had in my code that was breaking it, but I'll find out as I add things back, if needed. Thanks! can that action be set on the user's name instead of having an icon? I know the icon makes the action clearer, but the main/only reason for this widget is impersonation.
Dennis Koch
Dennis Koch2y ago
You could try TextColumn::make('name')->action(Impersonate::make()) Not sure whether that works out of the box though
usmcgator
usmcgatorOP2y ago
I was just about to post that exact thing. It did work out of the box:
protected function getTableColumns(): array
{
return [
Tables\Columns\TextColumn::make('name')->label('Name')->action(Impersonate::make('impersonate')),
Tables\Columns\TextColumn::make('email')->label('E-Mail'),
];
}
protected function getTableColumns(): array
{
return [
Tables\Columns\TextColumn::make('name')->label('Name')->action(Impersonate::make('impersonate')),
Tables\Columns\TextColumn::make('email')->label('E-Mail'),
];
}
Dennis Koch
Dennis Koch2y ago
Nice 👌🏼
Want results from more Discord servers?
Add your server