can't get trashed users to show up in select2_from_ajax field #1139
-
In my User CRUD I have this: CRUD::field([
'name' => 'spouse',
'label' => 'Spouse',
'type' => 'select2_from_ajax',
'entity' => 'spouse',
'attribute' => 'lastNameFirstName',
'data_source' => backpack_url('user/fetch/spouse'),
'method' => 'POST',
]); Here' my FetchOperation:
And here's my spouse method in my User model: public function spouse()
{
return $this->belongsTo(User::class, 'spouseID')->withTrashed();
} With all of this I can add trashed spouses without issue BUT I just can't see trashed spouse when viewing non-trashed users. If I do Any ideas? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Based on the information provided in the knowledge sources, I can offer some insights and suggestions to address your issue with viewing trashed spouses for non-trashed users in the select2_from_ajax field.
protected function fetchSpouse(Request $request)
{
return $this->fetch([
'model' => User::class,
'searchable_attributes' => ['lastNameFirstName'],
'paginate' => 10,
'searchOperator' => 'LIKE',
'query' => function($model) {
return $model->withTrashed()->where(function($query) {
$query->whereNull('deleted_at')
->orWhereNotNull('deleted_at');
});
}
]);
} This modification ensures that both trashed and non-trashed spouses are included in the results.
protected function fetchSpouse(Request $request)
{
$exclude = $request->input('exclude');
return $this->fetch([
'model' => User::class,
'searchable_attributes' => ['lastNameFirstName'],
'paginate' => 10,
'searchOperator' => 'LIKE',
'query' => function($model) use ($exclude) {
return $exclude ?
$model->withTrashed()->where('id', '!=' , $exclude) :
$model->withTrashed();
}
]);
} Then, update your field definition to include the CRUD::field([
'name' => 'spouse',
'label' => 'Spouse',
'type' => 'select2_from_ajax',
'entity' => 'spouse',
'attribute' => 'lastNameFirstName',
'data_source' => backpack_url('user/fetch/spouse?exclude=' . $this->crud->getCurrentEntry()->id),
'method' => 'POST',
]);
If these suggestions don't resolve your issue, it might be worth checking if there are any JavaScript errors in the browser console when the select2_from_ajax field is loaded or used, as suggested in the Github Issue #4907. |
Beta Was this translation helpful? Give feedback.
This was answer here
Cheers.