Ad

OcotberCMS PivotModel $attachOne Relation

I am trying to create many-to-many relation with a file attachment pivot in OcotberCMS.

Here's my relation

public $belongsToMany = [
    'users' => [
        User::class,
        'key' => 'task_id',
        'otherKey' => 'user_id',
        'table' => 'tasks_users',
        'pivot' => ['status'],
        'pivotModel' => TaskUser::class
    ]
];

In my pivotModel

class TaskUser extends Pivot
...
public $attachOne = [
    'file' => ['System\Models\File']
];

YAML config

pivot:
    form:
        tabs:
            fields:
                pivot[file]:
                    label: Image
                    type: fileupload
                    mode: image
                    span: left
                    tab: Image

The form is rendered correctly, but when trying to upload the file, the error is thrown: Upload error "A widget with class name 'relationUsersManagePivotFormPivotFile' has not been bound to the controller" on line 605 of D:\Projects\esport\modules\backend\classes\Controller.php

Ad

Answer

It seems it's kind of a lot of nesting which the relation manager is not able to handle. As well File does not work with differed binding when in used in pivot modal.

Relation manager with differed binding pivot data is not supported Ref: https://octobercms.com/docs/backend/relations#belongs-to-many-pivot

So maybe we can use an alternate way. if that is possible.

  1. In pivot table you can add new field file with text type
  2. Specify it in relation config => 'pivot' => ['status', 'file']

And now from the field

pivot:
    form:
        fields:
            pivot[value]:
                label: Value
                type: text
            pivot[file]:
                label: Picture
                type: mediafinder                    

Now you can upload and select files or select existing files. they will be stored as file path so they can directly be accessed.

if any doubt please comment.

Ad
source: stackoverflow.com
Ad