Validation error for multiple input field laravel 8

  javascript, laravel-8, php, php-7.4

I have a validation problem regarding multiple input field to store in Database. When i submit, the errors showing ‘additional mark field is required’. I try to test dd($request) but attributes is null.

How can i store in db with multiple input field ?

Here is my controller

public function StoreAdditionalProcuments(Request $request)
{

 $request->validate([
'additional_remark' => 'required',
  ]);

 foreach ($request->addmore as $key => $value) {
 $input = $request->all();
 $input['additional_remark']=$value['additional_remark'];
 AssetProcument::create($input);
}
        return redirect('asset')->with('success', 'Maklumat Aset berjaya disimpan.');
}

My model for assetprocument.php

class AssetProcument extends Model
{
  public $fillable = [

  'additional_remark',

  ];

blade

<form action="{{ route('asset_store_additional_procument') }}" method="POST">
                @csrf

                    <div class="card-body">
                        <div class="col">
                            <table class="table table-bordered" id="dynamicTable">
                                <thead>
                                    <tr>
                                        <th>Catatan</th>
                                    </tr>
                                </thead>
                                <tbody>
                                 
                                    <tr>
                                        <td style="width:50%"><textarea type="text" name="addmore[][additional_remark]" class="form-control"></textarea></td>
                                        <td><button type="button" name="add" id="add" class="btn btn-success">Tambah</button></td>
                                    </tr>
                                    
                                </body>
                            </table>
                        </div>
                            <div class="col-12">
                                <input type="submit" value="Save Changes" class="btn btn-success float-right">
                            </div> 
                    </div>
            </form>



<script type="text/javascript">

var i = 0;

$("#add").click(function(){

    ++i;

    $("#dynamicTable").append('<tr><td><textarea type="text" name="addmore['+i+'][additional_remark]"  class="form-control" /></textarea></td>'.'
    <td><button type="button" class="btn btn-danger remove-tr">Remove</button></td></tr>');
});

$(document).on('click', '.remove-tr', function(){
     $(this).parents('tr').remove();
});

route

Route::post('asset_store_additionalprocuments',[AssetController::class,'StoreAdditionalProcuments'])->name('asset_store_additional_procument');

Source: Ask PHP

LEAVE A COMMENT