laravel-datatables-editor
laravel-datatables-editor copied to clipboard
Pivot table support
Currently we do not have editor action to attach or detach record to Laravel pivot table, any plan to support it?
In my application, I have user groups and each user group can attach or detach members from it, I have this implementation currently and like to migrate to datatables editor.
$data->users()->updateExistingPivot($user->id, $validated);
I think you can already implement this using saved event hook. For instance, I have this role-permissions relationship and was able to save the pivots.

// Editor script
->editors(
Editor::make()
->fields([
Fields\Field::make('name'),
Fields\Field::make('slug')
->multiEditable(false)
->fieldInfo('If left blank, value will be generated based on name.'),
Fields\Checkbox::make('permissions[].id')
->modelOptions(Permission::class, 'name')
->label('Permissions'),
])
->language(Lang::get('datatables'))
);
Editor saved event hook:
/**
* @param Model|Role $model
* @param array $data
* @return Role
*/
public function saved($model, $data)
{
$model->syncPermissions(data_get($data, 'permissions.*.id', []));
return $model;
}
Thanks for sharing, it make sense. However, I decided to create a model for my pivot table, it helps simplify below actions:
- Retrieve JSON data from user group members table.
- Attach and detach group members, no additional code needed.
- Return updated data back to datatables, for updating display value.
The event hooks really useful for display related data on the pivot datatable. e.g. user name and group name.
public function saved(Model $model, array $data)
{
$model->setAttribute('user', $model->user);
$model->setAttribute('user_group', $model->userGroup);
return $model;
}
This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.
This issue was closed because it has been inactive for 7 days since being marked as stale.