exclude user from FetchOperation #1131
-
In my user management page spouses can be added. To facilitate this I'm using select2_from_ajax field with Fetch Operation. The basic idea is that you type another users name (last name, first name) and as you type an auto complete style dialog will show you users that resemble the one you've typed in. So if your spouse is already on the website you'll see her and be able to select her. The problem is that this auto complete style dialog includes the person who's spouse you're adding. eg. if you're editing "Doe, John" and are trying to add "Doe, Jane", after you've typed in "Doe" it'll show you both "Doe, John" and "Doe, Jane", when, ideally, it'd exclude "Doe, John" since it's "Doe, John"s profile that you're editing. The problem is that idk how to make it exclude a specific person. Here's the field in my UserCrudController: CRUD::field([
'name' => 'spouse',
'label' => 'Spouse',
'type' => 'select2_from_ajax',
'entity' => 'spouse',
'attribute' => 'lastNameFirstName',
'data_source' => backpack_url('user/fetch/spouse'),
'method' => 'POST',
]); Here's the FetchOperation method in that same controller: protected function fetchSpouse()
{
return $this->fetch([
'model' => User::class, // required
'searchable_attributes' => ['lastNameFirstName'],
'paginate' => 10, // items to show per page
'searchOperator' => 'LIKE',
'query' => function($model) {
return $model->withTrashed();
} // to filter the results that are returned
]);
} Any ideas? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I got this working - I replaced my protected function fetchSpouse(Request $request)
{
$exclude = $request->input('exclude');
return $this->fetch([
'model' => User::class, // required
'searchable_attributes' => ['lastNameFirstName'],
'paginate' => 10, // items to show per page
'searchOperator' => 'LIKE',
'query' => function($model) use ($exclude) {
return $exclude ?
$model->withTrashed()->where('id', '!=' , $exclude) :
$model->withTrashed();
} // to filter the results that are returned
]);
} |
Beta Was this translation helpful? Give feedback.
I got this working - I replaced my
data_source
wihbackpack_url('user/fetch/spouse?exclude=' . $this->crud->getCurrentEntry()->id),
and I updatedfetchSpouse
like so: