phplaravellaravel-filament

Im trying to pluck more than one value in PHP Filament v3


I am trying to pluck more than one value. I want firstname and lastname. How do i get both values to show up.

    SelectFilter::make('participant_id')
                    ->attribute('participant_id')
                    ->label(__('messages.participant_model_firstname'))
                    ->options([
                        array_unique(
                            Participant::all()
                                ->pluck('firstname', 'id')
                                ->toArray()
                        )
                    ]),
                

I found nothing really interesting on the internet to solve this problem.


Solution

  • first of all, don't call all() unless you want to return everything from the database, instead call pluck() on the eloquent query builder so that only the name and id columns are loaded, saving memory and cpu processing.

    Create a new column fullname by joining firstname and lastname:

    Participant::query()
      ->select([
        DB::raw("CONCAT(firstname, ' ', lastname) as fullname"),
        'id',
      ])
      ->pluck('fullname', 'id')
      ->toArray()