yajra / laravel-datatables-editor

Laravel DataTables Editor Integration.
https://yajrabox.com/docs/laravel-datatables/editor-installation
MIT License
115 stars 14 forks source link

Pivot table support #42

Closed bitrevo closed 5 months ago

bitrevo commented 4 years ago

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);
yajra commented 4 years ago

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;
    }
bitrevo commented 4 years ago

Thanks for sharing, it make sense. However, I decided to create a model for my pivot table, it helps simplify below actions:

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;
}
github-actions[bot] commented 5 months ago

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] commented 5 months ago

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