M
M
Mikhail A.2021-04-13 13:17:19
Laravel
Mikhail A., 2021-04-13 13:17:19

How to save additional pivot fields in Laravel?

I'm trying to implement a simple calculation constructor - add a new line, select an element from the list in it and fill in the quantity and price.

The pivot-table (calculation_element) is responsible for linking elements to the calculation, which has two more additional ones. fields (quantity, price).

With a single row, saving works, but how do you save multiple rows? Complicates the task of additional fields in the pivot table (quantity, price).

<tr>
    <td>
        <select v-model="formElement" class="custom-select">
            <option v-for="element in elements" :key="element.id" :value="element.id">{{ element.title }}</option>
        </select>
    </td>
    <td>
        <input v-model="formAmount" type="text" class="form-control" value="1"/>
    </td>
    <td>
        <input v-model="formPrice" type="text" class="form-control" value="1"/>
    </td>
</tr>
...
data() {
    return {
        elements: [],
        formElement: '',
        formAmount: '',
        formPrice: '',
    }
},
...
methods: {
    CalculationSubmit() {
        axios.post('/api/calculations', {
            formElement: this.formElement,
            formAmount: this.formAmount,
            formPrice: this.formPrice
        })
...

Controller (store)
$calculation->elements()->attach($data['formElement'],
    [
        'amount' => $data['formAmount'],
        'price' => $data['formPrice']
    ]
);

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question