Skip to content
Advertisement

Laravel multiple foreach for inserting data into database [closed]

I have a table with form input inside it. Whenever I click the submit button, it will save all of the data into the database like this.
enter image description here

What should I do inside my Laravel Controller? Thanks in advance. Here’s my code example

UPDATE Result:

enter image description here

UPDATE HTML Form Input:

<form action="/test-submit" method="post">
      @csrf
      <table id="example2" class="presence-table table table-bordered table-hover">
        <thead>
          <tr>
            <th class="align-middle" rowspan="2">#</th>
            <th class="align-middle" rowspan="2">Name</th>
            <th class="align-middle" colspan="6">Date</th>
          </tr>
          <tr>
            <th class="align-middle">1 Jan</th>
            <th class="align-middle">2 Jan</th>
            <th class="align-middle">3 Jan</th>
            <th class="align-middle">4 Jan</th>
            <th class="align-middle">5 Jan</th>
            <th class="align-middle">6 Jan</th>
          </tr>
        </thead>
        <tbody>
          @php
          $employees=["Gunawan","Roy"];
          $dates = ["1 Jan", "2 Jan", "3 Jan", "4 Jan", "5 Jan", "6 Jan"];
          @endphp
          @foreach($employees as $key=>$value)
          <tr class="data-row">
            <td class="align-middle iteration"></td>
            <input type="hidden" name="row[{{$key}}][employee_name]" value="{{$value}}">
            <td>{{ $value }}</td>
            @foreach ($dates as $date)
            <td>
              <div class="form-group">
                <select class="form-control" name="row[{{$key}}][presence][{{ $loop->iteration }}][presenceType]" required>
                  <option value="">-- Select --</option>
                  <option value="1">Full</option>
                  <option value="0.5">Half</option>
                  <option value="0">Zero</option>
                </select>
              </div>
            </td>
              
            <input type="hidden" name="row[{{$key}}][presence][{{ $loop->iteration }}][presencedate]" value="{{ $date }}">
            @endforeach
          </tr>
        @endforeach
        </tbody>
      </table>
      <button type="submit" class="float-right btn btn-info">Save</button>
    </form>

UPDATE Controller:

$data= collect($request->row)->map(function ($row,$key)use($request){

        return collect($row['presence'])->map(function ($presence)use($row){

            return ['name'=>$row['employee_name'],'value'=>$presence['presenceType'],'date'=>$presence['presencedate']];
        });
    })->flatten(1)->toArray();
    dd($data);

Advertisement

Answer

Not sure from where date will get but for other data first change form design

<form action="{{route("test")}}" method="post">
    @csrf
    <table id="example2" class="presence-table table table-bordered table-hover">
        <thead>
        <tr>
            <th class="align-middle" rowspan="2">#</th>
            <th class="align-middle" rowspan="2">Name</th>
            <th class="align-middle" colspan="6">Date</th>
        </tr>
        <tr>
            <th class="align-middle">1 Jan</th>
            <th class="align-middle">2 Jan</th>
            <th class="align-middle">3 Jan</th>
            <th class="align-middle">4 Jan</th>
            <th class="align-middle">5 Jan</th>
            <th class="align-middle">6 Jan</th>
        </tr>
        </thead>
        <tbody>

        @php

        $employee=["Gunawan","Roy"];
        @endphp
        @foreach($employee as $key=>$value)
        <tr class="data-row">
            <td class="align-middle iteration"></td>
            <input type="hidden" name="row[{{$key}}][employee_name]" value="{{$value}}">
            <td>Gunawan</td>
            <td>
                <div class="form-group">
                    <select class="form-control" name="row[{{$key}}][presence][1]" required>
                        <option value="">-- Select --</option>
                        <option value="1">Full</option>
                        <option value="0.5">Half</option>
                        <option value="0">Zero</option>
                    </select>
                </div>
            </td>
            <td>
                <div class="form-group">
                    <select class="form-control" name="row[{{$key}}][presence][2]" required>
                        <option value="">-- Select --</option>
                        <option value="1">Full</option>
                        <option value="0.5">Half</option>
                        <option value="0">Zero</option>
                    </select>
                </div>
            </td>
            <td>
                <div class="form-group">
                    <select class="form-control" name="row[{{$key}}][presence][3]" required>
                        <option value="">-- Select --</option>
                        <option value="1">Full</option>
                        <option value="0.5">Half</option>
                        <option value="0">Zero</option>
                    </select>
                </div>
            </td>
            <td>
                <div class="form-group">
                    <select class="form-control" name="row[{{$key}}][presence][4]" required>
                        <option value="">-- Select --</option>
                        <option value="1">Full</option>
                        <option value="0.5">Half</option>
                        <option value="0">Zero</option>
                    </select>
                </div>
            </td>
            <td>
                <div class="form-group">
                    <select class="form-control" name="row[{{$key}}][presence][5]" required>
                        <option value="">-- Select --</option>
                        <option value="1">Full</option>
                        <option value="0.5">Half</option>
                        <option value="0">Zero</option>
                    </select>
                </div>
            </td>
            <td>
                <div class="form-group">
                    <select class="form-control" name="row[{{$key}}][presence][6]" required>
                        <option value="">-- Select --</option>
                        <option value="1">Full</option>
                        <option value="0.5">Half</option>
                        <option value="0">Zero</option>
                    </select>
                </div>
            </td>
        </tr>
        @endforeach
        </tbody>
    </table>
    <button type="submit" class="float-right btn btn-info">Save</button>
</form>

and in controller

 $data= collect($request->row)->map(function ($row,$key)use($request){

           return collect($row['presence'])->map(function ($presence)use($row){

               return ['name'=>$row['employee_name'],'value'=>$presence];
           });
       })->flatten(1)->toArray();

and for insert to database

ModelName::insert($data);

Updated

You can do somethink like below for each date

<td>
                <div class="form-group">
                    <select class="form-control" name="row[{{$key}}][presence][1]['presenceType']" required>
                        <option value="">-- Select --</option>
                        <option value="1">Full</option>
                        <option value="0.5">Half</option>
                        <option value="0">Zero</option>
                    </select>
                </div>
            </td>
            <td>
                <div class="form-group">
                    <input type="text" name="row[{{$key}}][presence][1]['presencedate']" value="">
                </div>
            </td>

and in controller

  return ['name'=>$row['employee_name'],'value'=>$presence['presenceType'],'dae'=>$presence['presencedate']];

Update2

<form action="{{route("test")}}" method="post">
    @csrf
    <table id="example2" class="presence-table table table-bordered table-hover">
        <thead>
        <tr>
            <th class="align-middle" rowspan="2">#</th>
            <th class="align-middle" rowspan="2">Name</th>
            <th class="align-middle" colspan="6">Date</th>
        </tr>
        <tr>
            <th class="align-middle">1 Jan</th>
            <th class="align-middle">date</th>
            <th class="align-middle">2 Jan</th>
            <th class="align-middle">3 Jan</th>
            <th class="align-middle">4 Jan</th>
            <th class="align-middle">5 Jan</th>
            <th class="align-middle">6 Jan</th>
        </tr>
        </thead>
        <tbody>

        @php

            $employee=["Gunawan","Roy"];
 $dates = ["1 Jan", "2 Jan", "3 Jan", "4 Jan", "5 Jan", "6 Jan"];
        @endphp
        @foreach($employee as $key=>$value)
            <tr class="data-row">
                <td class="align-middle iteration"></td>
                <input type="hidden" name="row[{{$key}}][employee_name]" value="{{$value}}">
                <td>Gunawan</td>
                <td>
                    <div class="form-group">
                        <select class="form-control" name="row[{{$key}}][presence][1][presenceValue]" required>
                            <option value="">-- Select --</option>
                            <option value="1">Full</option>
                            <option value="0.5">Half</option>
                            <option value="0">Zero</option>
                        </select>
                    </div>
                </td>
                <td>
                    <div class="form-group">
                        <select class="form-control" name="row[{{$key}}][presence][1][presenceDate]" required>

                            <option value="">-- Select --</option>
                            @foreach($dates as $date)
                                <option value="{{$date}}">{{$date}}</option>
                            @endforeach

                        </select>
                    </div>
                </td>
                <td>
                    <div class="form-group">
                        <select class="form-control" name="row[{{$key}}][presence][2][presenceValue]" required>
                            <option value="">-- Select --</option>
                            <option value="1">Full</option>
                            <option value="0.5">Half</option>
                            <option value="0">Zero</option>
                        </select>
                    </div>
                </td>
                <td>
                    <div class="form-group">
                        <select class="form-control" name="row[{{$key}}][presence][2][presenceDate]" required>

                            <option value="">-- Select --</option>
                            @foreach($dates as $date)
                                <option value="{{$date}}">{{$date}}</option>
                            @endforeach

                        </select>
                    </div>
                </td>
                <td>
                    <div class="form-group">
                        <select class="form-control" name="row[{{$key}}][presence][3][presenceValue]" required>
                            <option value="">-- Select --</option>
                            <option value="1">Full</option>
                            <option value="0.5">Half</option>
                            <option value="0">Zero</option>
                        </select>
                    </div>
                </td>
                <td>
                    <div class="form-group">
                        <select class="form-control" name="row[{{$key}}][presence][3][presenceDate]" required>

                            <option value="">-- Select --</option>
                            @foreach($dates as $date)
                                <option value="{{$date}}">{{$date}}</option>
                            @endforeach

                        </select>
                    </div>
                </td>
                <td>
                    <div class="form-group">
                        <select class="form-control" name="row[{{$key}}][presence][4][presenceValue]" required>
                            <option value="">-- Select --</option>
                            <option value="1">Full</option>
                            <option value="0.5">Half</option>
                            <option value="0">Zero</option>
                        </select>
                    </div>
                </td>
                <td>
                    <div class="form-group">
                        <select class="form-control" name="row[{{$key}}][presence][4][presenceDate]" required>

                            <option value="">-- Select --</option>
                            @foreach($dates as $date)
                                <option value="{{$date}}">{{$date}}</option>
                            @endforeach

                        </select>
                    </div>
                </td>
                <td>
                    <div class="form-group">
                        <select class="form-control" name="row[{{$key}}][presence][5][presenceValue]" required>
                            <option value="">-- Select --</option>
                            <option value="1">Full</option>
                            <option value="0.5">Half</option>
                            <option value="0">Zero</option>
                        </select>
                    </div>
                </td>
                <td>
                    <div class="form-group">
                        <select class="form-control" name="row[{{$key}}][presence][5][presenceDate]" required>

                            <option value="">-- Select --</option>
                            @foreach($dates as $date)
                                <option value="{{$date}}">{{$date}}</option>
                            @endforeach

                        </select>
                    </div>
                </td>
                <td>
                    <div class="form-group">
                        <select class="form-control" name="row[{{$key}}][presence][6][presenceValue]" required>
                            <option value="">-- Select --</option>
                            <option value="1">Full</option>
                            <option value="0.5">Half</option>
                            <option value="0">Zero</option>
                        </select>
                    </div>
                </td>
                <td>
                    <div class="form-group">
                        <select class="form-control" name="row[{{$key}}][presence][6][presenceDate]" required>

                            <option value="">-- Select --</option>
                            @foreach($dates as $date)
                            <option value="{{$date}}">{{$date}}</option>
                            @endforeach

                        </select>
                    </div>
                </td>
            </tr>
        @endforeach
        </tbody>
    </table>
    <button type="submit" class="float-right btn btn-info">Save</button>
</form>

and in controller

$data= collect($request->row)->map(function ($row,$key)use($request){

           return collect($row['presence'])->map(function ($presence)use($row){

               return ['name'=>$row['employee_name'],'value'=>$presence['presenceValue'],'date'=>$presence['presenceDate']];
           });
       })->flatten(1)->toArray();
       dd($data);
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement