I am trying to store repeater data in database Using laravel. But for that I need to combine some data with different values.
my form looks like this--
<div v-for="(tab, tabIndex) in tabs" :key="tab.id">
<div class="col-md-4 offset-md-4 d-flex">
<label>HouseAreaTypes</label>
</div>
<div class="col-md-4 offset-md-4 d-flex">
<select
v-model="tab.selectedHouseType"
@change="getDecor(tabIndex)"
name="houseAreaTypeId">
<option v-for="houseType in houseTypes" :key="houseType.id" :value="houseType.id">{{ houseType.name }}</option>
</select>
<div class="col-md-4">
<button @click="addTab">
<i class="fa fa-plus"></i>
</button>
<button v-if="tabIndex > 0" class="btn btn-danger btn-sm mt-2 ml-2" @click="removeTab(tabIndex, tab)">
<i class="fa fa-minus"></i>
</button>
</div>
</div>
<div v-for="(row, rowIndex) in tab.rows" :key="row.id">
<table class="table table-borderless col-md-12">
<thead>
<th>DecorationType</th>
<th>Description</th>
<th class="col-md-2">Rate</th>
<th>Quantity</th>
<th class="col-md-2">TotalAmount</th>
<th class="col-md-2"></th>
</thead>
<tbody>
<td>
<select
v-model="row.selectedDecor"
@change="getDescription(tabIndex,rowIndex) "name="decId">
<option v-for="decorType in tab.decorTypes" :key="decorType.id" :value="decorType.id">
{{ decorType.name }}
</option>
</select>
</td>
<td class="col-md-3">
<input type="text" name="des" v-model="row.selectedDes"/>
</td>
<td class="col-md-2">
<input type="number" name="rate" v-model="row.selectedRate" @change="calculateLineTotal(tabIndex,rowIndex)"
/>
</td>
<td class="col-md-1">
<input type="number" min="0" name="qty" v-model="row.selectedQty" @change="calculateLineTotal(tabIndex,rowIndex)"
/>
</td>
<td class="col-md-3">
<input
type="number" name="totalAmount" v-model="row.line_total"/>
</td>
<td>
<button v-if="rowIndex == Object.keys(tab.rows).length -1" type="button" class="btn btn-success btn-sm mt-2 ml-2" @click="addRow(tabIndex)">
<i class="fa fa-plus"></i>
</button>
<button type="button"
@click="removeRow(rowIndex, row,tabIndex)">
<i class="fa fa-minus"></i>
</button>
</td>
</tbody>
</table>
<hr />
</div>
</div>
my migration looks like this--
Schema::create('house_area_carts', function (Blueprint $table) {
$table->id();
$table->integer('houseAreaTypeId')->nullable();
$table->text('houseAreaCartInfo')->nullable()->comment('Object (decorationTypeId, descriptionOfDecoration, qty, rate, totalAmount)');
$table->timestamps();
});
I already have done the frontend part using vue. Here I need to store multiple houseAreaTypeId but in the houseAreaCartInfo I need to commbine decorationTypeId, descriptionOfDecoration, qty, rate, totalAmount these datas. I am attaching an image so you understand well
updates
in my controller --
public function createCart(Request $request, $id)
{
$cart = HouseAreaCart::create([
'houseAreaTypeId' => $request->houseAreaTypeId,
'houseAreaCartInfo' => $request->get('houseAreaCartInfo'),
'houseAreaCartInfo' =>[
'rate' => $request->rate,
'qty' => $request->qty,
'decId' => $request->decId,
'des' => $request->des,
'totalAmount' => $request->totalAmount
]
]);
dd($cart);
return response()->json($cart);
}
I am getting this after dd.
"houseAreaTypeId" => "2"
"houseAreaCartInfo" => "{"rate":["11","2"],"qty":["8","1"],"decId":["2","3"],"des":["Nostrum dolorem qui","Veniam architecto l"],"totalAmount":["88.00","2.00"]}"
But when I add another houseAreaTypeId it's showing only the last houseAreaTypeId I added.
Now how do I process multiple houseAreaTypeId in my controller and save it in the database?