php - I want my selectEnhenced form to show a searched name with its type next to it as a hint

I have this form - for example a form for a new city - with some different selects and inputs. In one of them a user have to choose a commune - a commune for that particular city.

Before, on the select list, there was only a name of a commune, without the type - everything worked perfectly - but then I realised that to choose the right commune, the user need to know it's type too.

What I'm trying to make is something like this: when a user will type a name of a commune, it will show a list of available communes - the name of the commune with the type of that commune next to it:

see the pic

I tried to:

  • change my model - in many different ways - for example by creating a new function that would contain two others;
  • modify ajax - make something similar as in return?

But nothing works. In general I get a 500 server error because I want to put two "names" in one select. Is it possible to make it work the way I want?

Here are examples of my code:

  • FormTrait:

              $form->selectEnhanced('commune_id', trans('dict.commune'))
                  ->options(function ($id) {
                      $id = $id ? $id : old('commune_id');
                      if ($id) {
                          $commune = Commune::find($id);
                          return [$commune->id => $commune->communetranslation->name . ' (' . $commune->communetypetranslation->name . ')'];
                      }
                  })
                  ->ajax('/' . config('admin.route.prefix') . '/api/communes', 'id', 'communetranslation.name')
                  ->loadParent('district_id', '/' . config('admin.route.prefix') . '/api/districts');
    
  • Model:

     public function communetranslation()
     {
      return $this->getRelationDefinition($this->hasOne(CommuneTranslation::class, 'commune_id', 'commune_id'), 'commune_translations', 'commune_id');
     }
    
     public function communetypetranslation()
     {
      return $this->getRelationDefinition($this->hasOne('App\Models\CommuneTypeTranslation', 'commune_type_id', 'id'), 'commune_type_translations', 'commune_type_id');
     }
    

Answer

Solution:

It's not my solution - it's my colleague that came up with it. I decided to put it here as it might be helpful for others who are facing same problem as me.

So in the end the first idea was a good one. Almost. Instead of combining two functions we created a new one which contains same information as the two others together. We just needed to use first() in it. Also I didn't use the correct syntaxes so obviously it didn't work out. Anyway, It should look like this:

public function getCommunenamenandtype() {
    return "{$this->communetranslation()->first()->name} ({$this->communetypetranslation()->first()->name})";
}

I got exactly the outcome I wanted (shown on the picture I have attached to my question). :-) Hope it will be helpful for You as well.

Source