插入多个laravel复选框数据表

时间:2019-06-08 06:13:53

标签: laravel

我想在数据表中插入检查的多行,当我单击按钮验证器时,每个人在laravel框架中遇到问题,我想在单击按钮验证时在数据表中插入行检查,这是我的代码
工资表的显示

<body>
    <div class="container" id="app">
      <div class="list-group">
       <div class="list-group-item">

    <h3>Pointage Mensuel</h3>
    <div class="col-md-6 col-md-offset-3">
        <h3>jour : {{$data['datek']}} chantier : {{$data['chantier_name']}}</h3>
    </div>
    <button class="btn btn-success add-all" data-url="">Valider Pointage de mois</button>
</div>
</div>

  <div class="list-group">
   <div class="list-group-item">
    <table class="table table-bordered">
        <tr>
            <th>Archive</th>
            <th><input type="checkbox" id="check_all"></th>
            <th>S.No.</th>
            <th>matricule</th>
            <th>nom & prenom</th>
            <th>salaire net</th>
            <th>nbre de jour </th>
            <th>prime</th>
        </tr>
        @if($salaries->count())
            @foreach($salaries as $key => $salarie)
                <tr id="tr_{{$salarie->id}}">
                  <td>archive</td>
                  <td><input type="checkbox" class="checkbox" data-id="{{$salarie->id}}"></td>
                  <td>{{ ++$key }}</td>
                  <td>{{ $salarie->matricule }}</td>
                  <td>{{ $salarie->nom }} {{ $salarie->prenom }}</td>
                  <td>{{ $salarie->salairenet }}</td>
                  <td><input type="text" name="nbreJ" class="form-control" value="{{$data['nbr']}}"></td>
                  <td><input type="text" name="prime" class="form-control" value="0"></td>
                </tr>
            @endforeach
        @endif
    </table>
 </div>
</div>
<!-------------------//////////////////////////------------->
</div> 
</body>

为所有选中的/ uncheck和

代码编写ajax
<script type="text/javascript">
    $(document).ready(function () {
        $('#check_all').on('click', function(e) {
         if($(this).is(':checked',true)) {
            $(".checkbox").prop('checked', true);  
         } else {  
            $(".checkbox").prop('checked',false);  }  });
         $('.checkbox').on('click',function(){
            if($('.checkbox:checked').length == $('.checkbox').length){
                $('#check_all').prop('checked',true);
            }else{
                $('#check_all').prop('checked',false); }});
        $('.add-all').on('click', function(e) {
            var idsArr = [];  
            $(".checkbox:checked").each(function() {  
                idsArr.push($(this).attr('data-id'));});  
            if(idsArr.length <=0)  {  
                alert("Please select atleast one record to pointer.");  
            }  else {  
                    var strIds = idsArr.join(","); 
                    $.ajax({
                        url: "{{ route('salarie.multiple-add') }}",
                        type: 'POST',
                        headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                        data: {
                            'ids' : strIds},
                        success: function (data) {
                            if (data['status']==true) {
                                $(".checkbox:checked").each(function() { 
                                    alert(strIds); });
                                alert(data['message']);
                            } else {
                                alert('Whoops Something went wrong!!');}
                            window.location.reload()},
                        error: function (data) {
                            alert(data.responseText);}});}  }); });
</script>

功能控制器addMultiple

   public function addMultiple(Request $request){
        $pointage=new Pointage();

        $pointage->datep=$request->datep;
        $pointage->nbrj=$request->nbrj;
        $pointage->prime=$request->prime;
        $pointage->solde=$request->solde;
     return response()->json(['status'=>true]); 
    }

1 个答案:

答案 0 :(得分:0)

我很忙的时候,迟到笔记本电脑的道歉对我死了,但是您可以使用的一种方法是使用数组名,例如:

 <td><input type="checkbox" class="checkbox" name="row[$key][salarie]" data-id="{{$salarie->id}}"></td>

明确地说,如果您有多个输入且属于同一组,则它将在您的后端上形成一组输入,您可以循环这些输入。测试这个dd(request());在您的控制器功能之上。那么您应该能够在控制台中看到它返回的内容。

foreach(request(inputgroup) as $value){

   Pointage::create([
      'some_column' => $value['actualInputName']
   ]);
}

将您的功能更新为以下内容:

public function addMultiple(Request $request){
    dd(request());

    $pointage=new Pointage();

    foreach(request('row') as $row){
       // this is the important line $row is your request and ['salari'] is the name of the input   
       $pointage->salarie = $row['salarie'];
       $pointage->save();      
     }

     return response()->json(['status'=>true]); 
}