issue with pluck() on array
I’m trying to display a list of options according to the place name from database (option from options -json- as you could see in the image)
But it’s showing me the error message “Call to a member function pluck() on array”
Code:
Forms\Components\Select::make('placeName')
->label('Place Name')
->required()
->live()
->searchable()
->options(Place::all()->pluck('placeName'))
->afterStateUpdated(function ($state, callable $set, callable $get){
$place = $get('placeName');
$placePrice = Place::find($place)->placePrice;
$placeOptions = Place::find($place)?->options->pluck('option');
$set('totalPrice', $placePrice);
$set('options', $placeOptions);
})
Forms\Components\Select::make('placeName')
->label('Place Name')
->required()
->live()
->searchable()
->options(Place::all()->pluck('placeName'))
->afterStateUpdated(function ($state, callable $set, callable $get){
$place = $get('placeName');
$placePrice = Place::find($place)->placePrice;
$placeOptions = Place::find($place)?->options->pluck('option');
$set('totalPrice', $placePrice);
$set('options', $placeOptions);
})
2 Replies
you cant pluck from array
$placeOptions = Place::find($place)?->options;
// check if not null
$getPlaceOptions = collect($placeOptions)->pluck('option');
$placeOptions = Place::find($place)?->options;
// check if not null
$getPlaceOptions = collect($placeOptions)->pluck('option');
Indeed, wrote it this way and it worked
Thank you!
->options(function ($state, callable $set, callable $get) {
$place = Place::find($get('placeName'));
if ($place) {
$placeOptions = $place->options;
if ($placeOptions) {
$getPlaceOptions = collect($placeOptions)->pluck('option');
return $getPlaceOptions;
}
}
return null;
})
->options(function ($state, callable $set, callable $get) {
$place = Place::find($get('placeName'));
if ($place) {
$placeOptions = $place->options;
if ($placeOptions) {
$getPlaceOptions = collect($placeOptions)->pluck('option');
return $getPlaceOptions;
}
}
return null;
})