1K412
1K412

Reputation: 47

How to insert multiple row from form in laravel 8

I have a table in a form similar to this:

products comments
product 1 comment 1
product 2 comment 2

I want, when user click on save button, each row is added in database as it is in the form (as pairs [product i, comment i])

I tried

foreach ($request->products as $key=>$product) {
            $saveData = [
                'product' => $request->product[$key],
                'comment' => $request->comment[$key],
            ];
            DB::table('prod_com')->insert($saveData);
        }

in controller and

<form id="regForm" method="post" >
 @csrf
<table>
  <tr>
    <th>Product</th>
    <th>Comment</th>
  </tr>
  @foreach($prdcts as $products)
  <tr>
    <td>{{$products['product']}}</td>
    <td>
      <input type="text" class="form-control" name="comment[]" >
    </td>
  </tr>
</table>

in form but either data isn't added in database or i get a 405 error Method Not Allowed..

Any help is highly appreciated !

Edit : Full html form

<form id="regForm" action="{{route('saved')}}" method="post" >
       @csrf    
       <table class="table table-responsive table-condebsed table-hover">
           <thead>
             <tr>
              <th scop="col"> Product </th>
              <th class="col-md-6" scop="col"> Comment </th>
             </tr>
            </thead>
            <tbody>
              @foreach($prdcts as $products)
              <tr>
               <td>{{ $products['product'] }}</td>
               <td>
                 <input type="text" class="form-control" name="comment[]" >
               </td>
              </tr>
             @endforeach
            </tbody>
           </table>
           <br>
           <button type="submit" class="btn btn-sm btn-success">save</button>
</form>

& route :

Route::post('/saved', [App\Http\Controllers\ProdComController::class, 'submitData'])->name('saved');

Upvotes: 0

Views: 2550

Answers (2)

John Lobo
John Lobo

Reputation: 15319

Change html code as below

 @foreach($prdcts as $key=>$products)
                    <tr>
                        <td>{{$products['product']}}</td>
                        <td>
                            <input type="hidden" class="form-control" name="product[{{ $key}}][product]" value="{{$products['id']}}">
                            <input type="text" class="form-control" name="product[{{ $key}}][comment]" >
                        </td>
                    </tr>
                @endforeach

then in controller

 DB::table('prod_com')->insert($request->product);

if you get any error then try to print $request like below

dd($request->all());

so you can check data format in the request.If still issue then let me know in the comment

Upvotes: 0

zeeshan tariq
zeeshan tariq

Reputation: 119

you can use for loop while saving your data into database

for ($i = 0; $i < count($request->comment); $i++) {
       $comment = Commet::create([
            
            'comment' => $request->comment[$i],
        ]);
    }

Upvotes: 0

Related Questions