F
Filament3mo ago
jjo63

GetStateUsing - when can it be used? One example that works, one that doesn't

Hi all, GetStateUsing - have found this useful - here is an example of it being used to retrieve the description from an array - and this works beautifully
public static function table(Table $table): Table
{
// populate array of folders
$folders = DB::table('arch_folders')
->orderBy('id')
->pluck('name', 'id')
->toArray();
return $table
->columns([

Tables\Columns\TextColumn::make('name')
->label('Filename')
->description(function ($record, $state) use ($folders) {

return 'Folder: ' . $folders[$record->arch_folder_id] ?? null;
})
->sortable(),
public static function table(Table $table): Table
{
// populate array of folders
$folders = DB::table('arch_folders')
->orderBy('id')
->pluck('name', 'id')
->toArray();
return $table
->columns([

Tables\Columns\TextColumn::make('name')
->label('Filename')
->description(function ($record, $state) use ($folders) {

return 'Folder: ' . $folders[$record->arch_folder_id] ?? null;
})
->sortable(),
In this case we are displaying the name of a file and underneath (in the description) pulling the name of the folder based on its entry in an array $folders I thought that I could make the folder name itself a column by doing this
public static function table(Table $table): Table
{
// populate array of folders


$folders = DB::table('arch_folders')
->orderBy('id')
->pluck('name', 'id')
->toArray();


return $table
->columns([
Tables\Columns\TextColumn::make('Folder')
->label('Folder')
->getStateUsing(function ($record, $state) use ($folders){
return $folders[$record->arch_folder_id] ?? null;
}
),
public static function table(Table $table): Table
{
// populate array of folders


$folders = DB::table('arch_folders')
->orderBy('id')
->pluck('name', 'id')
->toArray();


return $table
->columns([
Tables\Columns\TextColumn::make('Folder')
->label('Folder')
->getStateUsing(function ($record, $state) use ($folders){
return $folders[$record->arch_folder_id] ?? null;
}
),
When I run this version the browser just hangs eventually saying it's unable to get a response - and the php http server will not respond to any other requests until I kill & restart it. Is my use of GetStateUsing in example 2 incorrect? No errors reported in storage/logs/laravel.log nor in the php http server output. thanks - not critical (I can workaround) but thought I'd flag it to see if it's my error or ..., j
4 Replies
Dennis Koch
Dennis Koch3mo ago
Sounds like you are running in a loop somehow, but I don't know why. I'd try it this way:
>getStateUsing(function ($record, $state) {
$folders = once(fn () =>
DB::table('arch_folders')
->orderBy('id')
->pluck('name', 'id')
->toArray()
);

return $folders[$record->arch_folder_id] ?? null;
}
>getStateUsing(function ($record, $state) {
$folders = once(fn () =>
DB::table('arch_folders')
->orderBy('id')
->pluck('name', 'id')
->toArray()
);

return $folders[$record->arch_folder_id] ?? null;
}
LeandroFerreira
LeandroFerreira3mo ago
I think you can't inject $state in getStateUsing, use $record instead
Dennis Koch
Dennis Koch3mo ago
Oh yeah. That makes sense and is probably the cause of the loop. $state trying to getStateUsing() trying to get $state ...
jjo63
jjo633mo ago
I'll take a look and report back my findings to confirm - thanks Leandro & Dennis
Want results from more Discord servers?
Add your server