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

5 messages · Page 1 of 1 (latest)

vivid patio
#

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(),

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;
                    }
                ),

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

sudden oasisBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

modest cipher
#

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;
}
prisma hull
modest cipher