简体   繁体   中英

i want to insert data into database directly from a table (view) but when i click add btn only the first or the last row get insterted

here in this code i have passed the hardcoded value of month if i remove january it accepts december ie last row how to solve this issue

controller

public function storeLeave(Request $request){

      $admin= new Admin();
      $admin->employee_name=$request->input('employee_name');
      $admin->month=$request->input('month_JANUARY');
      $admin->earned_leave=$request->input('earned_leave_JANUARY');
      $admin->casual_leave=$request->input('casual_leave_JANUARY');
      $admin->sick_leave=$request->input('sick_leave_JANUARY');

      $admin->save();
  }

blade file (table)

<table class="table table-bordered table-striped text-center" id="adminTable">
    <thead>
    <tr>
        <th class="text-center">MONTHS</th>
        <th class="text-center">EARNED LEAVE</th>
        <th class="text-center">CASUAL LEAVE</th>
        <th class="text-center">SICK LEAVE</th>
    </tr>
    </thead>
    <tbody>
    @foreach($months as $month)
        <tr>
            <td> <input readonly value="{{$month}}-<?php echo date("Y"); ?>" name="month_{{$month}}" id="month" style="background:none; border: none;"> </td>
            <td> <input type="text" style="background:none; border: none " name="earned_leave_{{$month}}" id="earned_leave" ></td>
            <td> <input type="text"  style="background:none; border: none" name="casual_leave_{{$month}}" id="casual_leave"></td>
            <td> <input type="text"   style="background:none; border: none" name="sick_leave_{{$month}}" id="sick_leave"></td>

            <td>
                <input type="submit" class="btn btn-success" name="submit" id="add_leave"  value="ADD">
                <input type="hidden" name="_method" value="POST">
            </td>

        </tr>
        @endforeach
    </tbody>
</table>

try this

    @foreach($months as $month)
    <form action="POST">
    <tr>
    <td>
        <input type="hidden" value="{{$month}}-<?php echo date(" Y "); ?>" name="month" id="month" style="background:none; border: none;"> </td>
    <td>
        <input type="text" style="background:none; border: none " name="earned_leave" id="earned_leave">
    </td>
    <td>
        <input type="text" style="background:none; border: none" name="casual_leave" id="casual_leave">
    </td>
    <td>
        <input type="text" style="background:none; border: none" name="sick_leave" id="sick_leave">
    </td>

    <td>
        <input type="submit" class="btn btn-success" name="submit" id="add_leave" value="ADD">

    </td>

</tr>
    </form>
    @endforeach

$admin= new Admin();
$admin->employee_name=$request->input('employee_name');
$admin->month=$request->input('month');
$admin->earned_leave=$request->input('earned_leave');
$admin->casual_leave=$request->input('casual_leave');
$admin->sick_leave=$request->input('sick_leave');

$admin->save();

You must use a for loop inserting in database. The code below is the example on how you handle the for loop. Happy Coding.

       for($i=0; $i < count($request->employee_name); ++$i) {
          Admin::create([
            "employee_name" => $request->employee_name[$i],
         ]);
       }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM