lundi 25 janvier 2021

Can't update existing data on database

The goal of the system is to update and create companion details on the same field. There is no problem in storing new data but it can't update the existing ones. What am I missing here?

here's my controller to store and edit data:

  public function update(UpdateInsuranceRequest $request, Insurance $insurance)

    $lastid = $insurance->id;
        if(count($request->companion_name) > 0)
        {
        foreach($request->companion_name as $comp=>$v){
            $data2=array(
                'insurance_id'=>$lastid,
                'companion_name'=>$request->companion_name[$comp],
                'ic'=>$request->ic[$comp],
                'dob'=>$request->dob[$comp],
                'relationship'=>$request->relationship[$comp]
            );
            if ($request->companion_id[$comp] <> '')
            {
                    $compaud=Companion:: where('insurance_id', $insurance->id)->first();
                    $compaud->update($data2);
            }
            else
            {
                Companion::insert($data2);
            }
        }
    }
return redirect()->route('admin.insurances.index');
}

my edit blade

<div class="card-body">
<input type="text" name="cn" value="">
@endforeach --}}
<table class ="table" id="companions_tb">
    <thead>                                   
        <tr>
            <th>Name</th>
            <th>Date of Birth</th>
            <th>IC No</th>
            <th>Relationship</th>
        </tr>
    </thead>
    <tbody>
        @foreach (old('companions', $companions->count() ? $companions : ['']) as $companion)
       
        <tr id="companion">
            <tr> 
            <td>
                <input type="hidden" name="companion_id[]" id="companion_id[]" class="form-control" value="" />
                 <input type="text" name="companion_name[]" id="companion_name[]" class="form-control" value="" />
            </td>

            <td>
                <div class="input-group date" data-provide="datepicker" data-date-format="yyyy-mm-dd" data-date-today-highlight="true" data-date-end-date="0d">
                <input class="form-control" name="dob[]" id="dob[]" value="">
                <div class="input-group-addon">
                <span class="fa fa-calendar"></span>
                </div>
                </div>
            </td>
            
            <td>
                <input type="text" name="ic[]" id="ic[]" class="form-control" value="" />
            </td>
            <td>
                <input type="text" name="relationship[]" id="relationship[]" class="form-control" value="" />
            </td>
        </tr>
        @endforeach
        <tr id="companion"></tr>
        <tr id="companion"></tr>
    </tbody>
</table>

<div class="row">
    <div class="col-md-12">
        <input type="button" class="addRow btn btn-default" value="+ Add Row">
        <input type="button" class="btn btn-danger remove" value="+ Remove Row">
    </div>
</div>

<script>
    $('.addRow').on('click',function(){
        addRow();
    });
    function addRow()
    {
        var tr='<tr>'+
        '<td><input type="hidden" name="companion_id[]" id="companion_id[]"><input type="text" name="companion_name[]" id="companion_name[]" class="form-control"></td>'+ 
        '<td><div class="input-group date" data-provide="datepicker" data-date-format="yyyy-mm-dd" data-date-today-highlight="true" data-date-end-date="0d"><input class="form-control" name="dob[]" id="dob[]" value=""><div class="input-group-addon"><span class="fa fa-calendar"></span></div></div></td>'+
        '<td><input type="text" name="ic[]" id="ic[]" class="form-control" value=""></td>'+
        '<td><input type="text" name="relationship[]" id="relationship[]" class="form-control" value=""></td>'+
        '</tr>'
        $('tbody').append(tr);
    };

</script>

database: enter image description here

what the view looks like:

enter image description here



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3od6jiH
via IFTTT

Aucun commentaire:

Enregistrer un commentaire