Home > Blockchain >  How to catch all data from @foreach loop and pass it into <input> value? - laravel
How to catch all data from @foreach loop and pass it into <input> value? - laravel

Time:10-14

So im new to laravel and im trying to get a data from 'cart' table in database with @foreach, and i try this..

<form action="{{ url('/checkout') }}" method="post">
@method('POST')
@csrf

@foreach ( $datacart as $keycart)
<input type="hidden" value="{{ $keycart->nama_produk}}" name="nama_produk"/>
@endforeach

</form>

this is the cart table

and it does pass all the 'nama_produk' into 'input' value, but the problem is, when i try to insert all the 'cart' data into 'checkout' table in database, it only insert 1 data. and i try to check what data does it pass using dd(); , it only pass the latest row data from 'cart' table.

so is there a way to pass all the data that i get from @foreach and keep it into 'input' value?

and is there a best way to insert data into database without using input hidden? because in this condition i dont have choice

thank you!

CodePudding user response:

Change the input

<input type="hidden" value="{{ $keycart->nama_produk}}" name="nama_produk"/> 

to

<input type="hidden" value="{{ $keycart->nama_produk}}" name="nama_produk[]"/>

then you will get all the data in controller as an array.

then loop the data name_produk and insert to db

foreach($request->nama_produk as $row){
 your query here...
}
  • Related