mercredi 28 septembre 2022

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


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/Zt4XRAe
via IFTTT

Aucun commentaire:

Enregistrer un commentaire