I have a dynamic form that successfully adds multiple field by a click of the button. My problem comes when saving the data in the database. I want to be able to save in the employee_id
field the id
of the Auth::user
.
This is my current set of code. Should I use a different approach such as for loop
instead of foreach
?
Component.vue
<tr v-for="(row, index) in rows" :key="row.id">
<td><base-select
:items="department_objectives"
item-text="department_objective"
item-value="id"
label="Department Objectives"
/></td>
<td><v-textarea label="KPA" placeholder=" " class="mr-2" rows="1" outlined v-model="row.kpa" /></td>
<td><v-textarea label="KPI" placeholder=" " class="mr-2" rows="1" outlined v-model="row.kpi" /></td>
<td><v-text-field label="Weight" placeholder=" " class="mr-2" outlined v-model="row.weight" /></td>
<td><v-text-field label="Score" placeholder=" " class="mr-2" outlined :disabled="disabled" filled v-model="row.score" /></td>
<td><a @click="removeElement(index);" style="cursor: pointer">Remove</a></td>
</tr>
addRow () {
this.rows.push({
kpa: '',
kpi: '',
weight: '',
score: '',
equal: '',
});
save () {
axios
.post('/api/employee-objective', { data: this.rows })
.then(res => { console.log(res) })
.catch(err => { console.log(err) });
}
Controller.php
public function store(Request $request) {
foreach($request->data as $data) {
$container = EmployeeObjective::updateOrCreate([
'employee_id' => // insert ID
'kpa_info' => $data['kpa'],
'kpi_info' => $data['kpi'],
'kpa_weight' => $data['weight'],
'kpa_score_1' => $data['score'],
'kpa_equal' => $data['equal'],
]);
$container->save();
}
}