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:
I tried to:
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');
}
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.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.