select2_from_ajax / relationship - attribute made up of first_name, last_name #472
-
tldr - how can I use select2_from_ajax / relationship / fetch with a model with multiple fields (e.g. first_name, last_name) that make up the name? Hi, I have a Contact model with first_name & last_name fields. In another CrudController, I would like to use either a select2_from_ajax field type or a relationship field type (I've tried both). I can get it to show the results in the select2 ajax search: And it populates the field when I click on the option to select: It even saves it to the database correctly. However when I go back into the record, it doesn't show anything in the field - just the "clear" option: This is my code: $this->crud->addField([
'type' => "relationship",
'name' => 'contact',
'ajax' => true,
'label' => 'Contact',
'attribute' => 'name',
'data_source' => backpack_url('user/fetch-contacts')
]);
protected function fetchContacts()
{
return $this->fetch([
'model' => \App\Models\Contact::class,
'query' => function ($model) {
return $model->with('organisation')
->selectRaw("contacts.id, CONCAT(organisations.name, ' - ', COALESCE(contacts.title, ''), ' ', COALESCE(contacts.first_name, ''), ' ', COALESCE(contacts.last_name, '')) as name, organisations.name as org_name")
->leftJoin('organisations', 'organisations.id', '=', 'contacts.organisation_id')
->orderByRaw('organisations.name ASC, contacts.last_name ASC');
},
'searchable_attributes' => ['first_name', 'last_name'],
'distinct' => true,
]);
} Can anyone tell me what I'm doing wrong? Many thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Update I remember a similar case a year ago. If I remember correctly the issue is that the keys are not taken in consideration for the filtering. As a quick test you can do the following: protected function fetchContacts()
{
if (request()->get('keys')) {
return $this->fetch([
'model' => \App\Models\Contact::class,
'searchable_attributes' => ['first_name', 'last_name'],
]);
} else {
// your current logic
return $this->fetch([
'model' => \App\Models\Contact::class,
'query' => function ($model) {
return $model->with('organisation')
->selectRaw("contacts.id, CONCAT(organisations.name, ' - ', COALESCE(contacts.title, ''), ' ', COALESCE(contacts.first_name, ''), ' ', COALESCE(contacts.last_name, '')) as name, organisations.name as org_name")
->leftJoin('organisations', 'organisations.id', '=', 'contacts.organisation_id')
->orderByRaw('organisations.name ASC, contacts.last_name ASC');
},
'searchable_attributes' => ['first_name', 'last_name'],
'distinct' => true,
]);
}
} It will not show the exact text you want, but it will help in debugging the issue. I will try to find my sample and check the exact cause. Original response Do you have the |
Beta Was this translation helpful? Give feedback.
Update
I remember a similar case a year ago. If I remember correctly the issue is that the keys are not taken in consideration for the filtering. As a quick test you can do the following: