0

Heello everyone!

I have a product crud controller in my project and I want to assign specifications and their values (pivot table of the products and the specifications tables), and also the specification groups (pivot table of the products and the specification groups), to it simultaneously. I'm stuck in here, because I cannot have one subfields inside another. Appreciate any suggestions to solve this.

Here are the details:

My database structure is as follows (NO JSON COLUMNS IN DATABASE):

  • The products table has one-to-many relationship with the categories table
  • The categories table has many-to-many relationship with the specification_groups table
  • The specification_groups table has many-to-many relashionship with the specifications table
  • The specifications table has many-to-many relationship with the products table (the values of each product's specification will be inserted inside this pivot table)
  • And, the specification_groups table has many-to-many relationship with the products table Database structure

I tried unsuccessfully to fill the last two pivot tables, using two relationships, as follows (I want that the Admin sees repeatable rows or tables rows of specification_group (select) -> specification (select) -> value (text field to be filled by the Admin)):

    CRUD::addField([
            'name' => 'specificationGroups',
            'label' => "Specifications",
            'type' => 'relationship',
            'init_rows' => 1,
            'min_rows' => 1,
            'tab' => 'Specifications',
            'pivotSelect' => [
                'entity' => 'specificationGroups',
                'model' => "App\Models\SpecificationGroup",
                'attribute' => 'name',
                'ajax' => true,
                'data_source' => backpack_url("product/fetch/specification-group"),
                'dependencies'  => ['category_id'],
                'method' => 'POST',
                'minimum_input_length' => 0,
                "include_all_form_fields" => true,
                'wrapper' => [
                    'class' => 'form-group col-md-6',
                ],
            ],
            'subfields' => [
                [
                    'name' => 'sort',
                    'type' => 'number',
                    'attributes' => ["min" => "1"],
                    'wrapper' => [
                        'class' => 'form-group col-md-6',
                    ],
                ],
                [
                    'name' => 'specifications',
                    'label' => "Specification",
                    'type' => 'relationship',
                    'init_rows' => 1,
                    'min_rows' => 1,
                    'pivotSelect' => [
                        'entity' => 'specifications',
                        'model' => "App\Models\Specification",
                        'attribute' => 'name',
                        'ajax' => true,
                        'data_source' => backpack_url("product/fetch/specification"),
                        'dependencies'  => ['category_id', 'specificationGroups'],
                        'method' => 'POST',
                        'minimum_input_length' => 0,
                        "include_all_form_fields" => true,
                    ],
                    'subfields' => [
                        [
                            'name' => 'value',
                            'type' => 'text',
                            'wrapper' => [
                                'class' => 'form-group col-md-12',
                            ],
                        ],
                        [
                            'name' => 'sort',
                            'type' => 'number',
                            'attributes' => ["min" => "1"],
                            'wrapper' => [
                                'class' => 'form-group col-md-12',
                            ],
                        ],
                    ],
                ],
            ],
        ]);

My preferred implementation (but with errors) Js errors

I rellay appreciate any suggestions for implementing this insert.

Rouhollah Joveini
  • 158
  • 1
  • 3
  • 14

1 Answers1

0

I think you need to extend your store and update methods, so you are having full control of the operation

public function store()
{
    $video = json_decode(request('video_field'), true)[0];

    $response = $this->traitStore();

    $this->crud->entry->video()->create($video);

    return $response;
};

public function update()
{
    $video = json_decode(request('video_field'), true)[0];

    $response = $this->traitUpdate();

    $this->crud->entry->video()->update($video);

    return $response;
}
Martin Tonev
  • 747
  • 12
  • 21