laravel-datatables-editor icon indicating copy to clipboard operation
laravel-datatables-editor copied to clipboard

Pivot table support

Open bitrevo opened this issue 6 years ago • 2 comments

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);

bitrevo avatar Oct 02 '19 04:10 bitrevo

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.

image

// 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;
    }

yajra avatar Oct 02 '19 06:10 yajra

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;
}

bitrevo avatar Oct 03 '19 03:10 bitrevo

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.

github-actions[bot] avatar Mar 19 '24 00:03 github-actions[bot]

This issue was closed because it has been inactive for 7 days since being marked as stale.

github-actions[bot] avatar Mar 26 '24 00:03 github-actions[bot]