Struggling with pluck
I have a table action row, which opens a modal. I´m able to access the $record, and when i do a dd on the $record, only the row-record is present. However, if i use the pluck method, it gives me the all the records on that model:
Gives me an unexpected result:
dd($record->pluck('name', 'id'))
Illuminate\Support\Collection {#2420 ▼ // app/Filament/Resources/CustomerResource.php:261
#items: array:3 [▼
1 => "Demobedriften"
2 => "Datterselskap"
3 => "Test testensen"
]
#escapeWhenCastingToString: false
}
Works as expected:
Forms\Components\TextInput::make('test')->default(fn ($record) => dd(($record->name)),
"Demobedriften" // app/Filament/Resources/CustomerResource.php:261
As i understand the documentation, i should be able to use the pluck method and only pluck fields from the selected row record. Is this by design or a bug? Is there other ways to pluck valus form a single row-record?2 Replies
pluck()
is an Eloquent method that returns data from the database. $record->pluck()
is the same as Customer::pluck()
.
What are you trying to do? This isn't a method you should call on a Model instance.That makes sense. I´m trying insert the selected row record as a option and default value in a select component, so basically what i need is a pluck metod on the row record
Forms\Components\Select::make('customer_id')
->label('Tilknyttet kunde')
->default(1)
->options(fn ($record) => $record->name)
])
I got it working using this code:
Forms\Components\Select::make('customer_id')
->label('Tilknyttet kunde')
->default(fn ($record) => $record->id)
->options((fn (Customer $record) => Customer::where('id', $record->id)->pluck('name', 'id')))
->searchable()
->getSearchResultsUsing(fn (string $search): array => Customer::where('name', 'like', "%{$search}%")->limit(50)->pluck('name', 'id')->toArray())
->getOptionLabelUsing(fn ($value): ?string => Customer::find($value)?->name),
])