Laravel backpack pro relationship field: add the same child twice in the same parent cant be limited #498
-
is there any way on relationship Pro fields to not allow having the same instance twice as a children in a belongsToMany relationship. Is there any to have some kind of unique constrain example here on stackoverflow: I can add the same POI twice. I tried to look on the doc and check the field blade view, I didn't find any option to filter the dropdown list |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hey @gillesboisson You can configure the "pivot select" using the You can make it an ajax field and then filter the previously selected options by sending the other form values with: You can then intercept the request on the in ajax endpoint (You can use There are plans to make this "easier" by having some option developers can set on the field definition like Let me know how it goes. Cheers |
Beta Was this translation helpful? Give feedback.
-
Thanks for your quick feedback, It works perfectly. Here is an example of my implementation // Fetch implementation
public function fetchPois(PoiListRequest $request)
{
// get all linked uuids from the request
$forms = $request->input('form');
$uuids = [];
foreach ($forms as $form) {
$formName = $form['name'];
if ($formName === 'uuid') {
$uuids[] = $form['value'];
}
}
return $this->fetch([
'model' => Poi::class, // required
'query' => function ($model) use ($uuids) {
return $model
->whereNotIn('uuid', $uuids);
}
]);
}
// ...
protected function setupUpdateOperation()
{
// ...
$this->crud->addField([
'tab' => trans($langPrefix . 'tabs.pois'),
'name' => 'pois',
'type' => "relationship",
'allows_multiple' => false,
'subfields' => [
[
'name' => 'trigger_radius',
'type' => 'number',
],
],
// allow to ignore poi which has already been added to the hub map
'pivotSelect' => [
"include_all_form_fields" => true,
'ajax' => true,
'placeholder' => 'Pick a poi',
'wrapper' => [
'class' => 'col-md-6',
],
],
]);
// ... // route setup
Route::prefix('/hubmap')->group(function () {
Route::post('/fetch/pois', 'HubMapCrudController@fetchPois'); // pivot select poi fetch
Route::crud('/', 'HubMapCrudController');
});
}); |
Beta Was this translation helpful? Give feedback.
Thanks for your quick feedback, It works perfectly. Here is an example of my implementation