Display custom attribute to user while using select2_from_ajax field with Fetch and InlineCreation #1147
-
Hi, In the database, I have two fields: contact_name and contact_phone. I want users to see “contact_name (contact_phone)” when selecting in select2_from_ajax. I’ve also enabled InlineCreation for the same field. By default, I couldn’t find a solution to use my full_contact_name attribute, which is defined in the model. I worked around this by using custom queries. For fetching:
For inline creation:
This works, but I’d like to ask if this is an appropriate solution? Or is there a better, smoother way to achieve the same result? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey @GregaUNK I think you will be better using Laravel Accessors/Mutators https://laravel.com/docs/11.x/eloquent-mutators#accessors-and-mutators and then add that as the select Basically define a I think adding the attribute to Cheers |
Beta Was this translation helpful? Give feedback.
Hey @GregaUNK I think you will be better using Laravel Accessors/Mutators https://laravel.com/docs/11.x/eloquent-mutators#accessors-and-mutators and then add that as the select
attribute
in the field.Basically define a
public function fullContactName(): Attribute
in your model and in the field, defineattribute => fullContactName
. Don't forget you need to add that attribute to model$appends
. https://laravel.com/docs/11.x/eloquent-serialization#appending-values-to-jsonI think adding the attribute to
$appends
it's what missing in your use case, and non of those overwrites would be needed.Cheers