lundi 8 octobre 2018

loop through all checkboxes then post status to database laravel 5.4

I have table like the below and i used eloquent to get all data based on key at my controller

my table contain duplication at Error column also Factor Column

Error    Factor    Attribute    PK_ID    Comment ErrorID  FactorID AttID
X1        YY        Att1          1
X1        YY        Att2          2 
X1        YY        Att3          3 
X2        ZZ        Att4          4 
X4        ZZ        Att5          5 

data represented as the below format

<h6>Error</h6>
  <h5>related Factor</h5>
   <type='checkbox'id='related attribute 1 value'>
   <textarea id='related attribute 1 value comment'></textarea>
   <type='checkbox'id='related attribute 2 value'>
   <textarea id='related attribute 2 value comment'></textarea>
    //... and so on 
 <h6>Error</h6>
     <h5>related Factor</h5>
   <input type='checkbox' id='related attribute 1 value'>
   <textarea id='related attribute 1 value comment'></textarea>
   <type='checkbox' id='related attribute 2 value'>
   <textarea id='related attribute 2 value comment'></textarea>
    //... and so on 

using foreach at my view to achieve that

 @foreach($errorForView as $error)
<h5></h5>
@foreach($error['factors'] as $factor)
    <h6></h6>

    @foreach($factor['attributes'] as $attr)
        <input type='checkbox' name='Att[]' value=''>
        <input type='textarea' name='comment[]' >
    @endforeach

  @endforeach
@endforeach

but when i try to post update or insert from the generated form to the database i tried to collect values from all requests by using [] array but what inserted to the database is wrong data with corrupted order so how can i loop through each checkbox then collect it's value which will represent to my attribute value & loop through each textarea to collect data to comment column at my table

        $num = count($request->input('Att'));
        for ($i = 0; $i < $num; $i++) {
        $att = (int)$request->input('Att')[$i];
        $rowz= table::where('id', '=', $SN)->first();
        if ($request->has('Att')[$i]) {
            $row->Value = 0;
        } else {
            $row->Value = 1;
            $row->Comment = $request->input('comment')[$i];
        }
        $row->save();
    }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire