how can i save additional field to a table using relationship field

36 views Asked by At

using the example from the backpack documentation,

i have the following tables:

invoices: id, number, due_date, payment_status, other fields... invoice_items: id, order, description, unit, quantity, unit_price

and the following fields:

CRUD::field([
            'type'=>'date_picker',
            'name'=>'due_date',
            'label'=>'Due Date', 
        ]);

<--- other fields --->

CRUD::field([
    'name'          => 'items',
    'type'          => "relationship",
    'subfields'   => [
        [
            'name' => 'order',
            'type' => 'number',
            'wrapper' => [
                'class' => 'form-group col-md-1',
            ],
        ],
        [
            'name' => 'description',
            'type' => 'text',
            'wrapper' => [
                'class' => 'form-group col-md-6',
            ],
        ],
        [
            'name' => 'unit',
            'label' => 'U.M.',
            'type' => 'text',
            'wrapper' => [
                'class' => 'form-group col-md-1',
            ],
        ],
        [
            'name' => 'quantity',
            'type' => 'number',
            'attributes' => ["step" => "any"],
            'wrapper' => [
                'class' => 'form-group col-md-2',
            ],
        ],
        [
            'name' => 'unit_price',
            'type' => 'number',
            'attributes' => ["step" => "any"],
            'wrapper' => [
                'class' => 'form-group col-md-2',
            ],
        ],
    ],
]);

how can i save the due_date field together with the relationship subfields in the invoice_items table?

i tried to override the store method but i get duplicate data in the invoice_items table

1

There are 1 answers

0
Mohammad Emran On

I would suggest creating a hidden "due_date" field within the subfields and updating them using the CrudFields JS library:

https://backpackforlaravel.com/docs/6.x/crud-fields-javascript-api

The way it would work:

crud.field('due_date').onChange(field => {
    // find the due_date in the subfields and update them
});

Let me know if this helps!