0

首先我很抱歉我的英语。我有一个带有动态 id 输入和复选框的表单,例如 checkbox[] , input name="pointage_j[] ...

控制器中的验证已完成,但表中的插入数据不如预期,例如当 pointage_j.1 = 1 和 pointage_j.2 = 1 时,所有插入都完美完成,但当 pointage_a.1 或 pointage_2.1 时,所有数据插入均使用第一个 id_employee 完成. 我不明白我的错误在哪里!

形式 :

@php
    $nbr = count($employees);
@endphp

{{Form::hidden("nbr", $nbr)}}

<div class="form-group">
    {{Form::label('date_pointage', 'Date du pointage')}}
    {{Form::date('date_pointage', '', ['class' => 'form-control', 'placeholder' => 'Date du pointage'])}}
</div>
<table class="table table-bordered">
    <thead>
    <tr>
        <th class="col-md-1 align-middle" scope="col">N°</th>
        <th class="col-md-2 align-middle">Nom Prénom</th>
        <th class="col-md-1 align-middle" scope="col">Présence_J</th>
        <th class="col-md-1 align-middle" scope="col">Présence_N</th>
        <th class="col-md-1 align-middle" scope="col">Absence</th>
        <th class="col-md-2 align-middle" scope="col">Heurs Sup</th>

    </tr>
    </thead>
    <tbody>
    @php $i = 0; @endphp
    @foreach($employees as $employee)
        @php $i = $i + 1; @endphp
        <tr>
            <th> @php echo $i ; @endphp</th>
            <td>{{$employee ->nom_employee}} {{$employee ->prenom_employee}}</td>
            <td>{{Form::checkbox("pointage_j[]", 1, 0,['id' => "pointage_j.$i"]) }}</td>
            <td>{{Form::checkbox("pointage_n[]", 1, 0,['id' => "pointage_n.$i"]) }}</td>
            <td>{{Form::checkbox("pointage_absence[]", 1, 0,['id' => "pointage_absence.$i"]) }}</td>
            <td>{{Form::text("heur_sup[]", '0', ['class' => 'form-control','id'=>"heur_sup.$i"])}}</td>

            {{Form::hidden("employee_id[]", $employee->id,['employee_id'=>"employee_id.$i"])}}
        </tr>

    @endforeach
    </tbody>
</table>

<button type="submit" class="btn btn-primary mb-2">Enregistrer</button>

{!! Form::close() !!}

在我的控制器中存储方法:

public function store(Request $request)
{
    $this->validate($request, [
        'nbr'                => 'required|integer',
        'employee_id.*'      => 'required|integer',
        'date_pointage'      => 'required|date',
        'pointage_j.*'       => 'accepted',
        'pointage_n.*'       => 'accepted',
        'pointage_absence.*' => 'accepted',
        'heur_sup.*'         => 'nullable|integer|between:0,5',
    ]);

    $nbr = $request->input('nbr');

    for ($i = 0; $i < $nbr; $i++) {
        if ($request->has("pointage_j.$i")) {
            $pointage_j = 1;
        } else {
            $pointage_j = 0;
        }

        if ($request->has("pointage_n.$i")) {
            $pointage_n = 1;
        } else {
            $pointage_n = 0;
        }

        if ($request->has("pointage_absence.$i")) {
            $absence = 1;
        } else {
            $absence = 0;
        }
        $pointage = new Pointage;
        $pointage->employee_id = $request->input("employee_id.$i");
        $pointage->date_pointage = $request->input("date_pointage");
        $pointage->jour_pointage = $pointage_j; //$request->has("pointage_j.$i") ? 1 : 0
        $pointage->nuit_pointage = $pointage_n;
        $pointage->absence_pointage = $absence;
        $pointage->heurs_sup_pointage = $request->input("heur_sup.$i");
        $pointage->save();
    }

    return redirect('/pointages')->with('success', 'Enregistrement reussi');
}

经过一天的折腾,我找到了解决方案。我使用了一个隐藏在复选框之前的输入字段,并且我使用 vanila javascript 来更改默认情况下隐藏的输入 0 的值,如果选中该复选框,则该值更改为 1。在我的控制器中,我忽略该复选框,我只取该值的隐藏输入。请注意,隐藏的输入必须在复选框之前,没有空格或换行符。我放了视图和控制器的代码,也许它对某人有用。

我的观点创建:

  {!! Form::open(['action' =>'PointagesController@store', 'method' => 'POST']) !!}
@csrf

@php
    $nbr = count($employees);
@endphp
{{Form::hidden("nbr", $nbr)}}

<div class="form-group">
    {{Form::label('date_pointage', 'Date du pointage')}}
    {{Form::date('date_pointage', '', ['class' => 'form-control', 'placeholder' => 'Date du pointage'])}}
</div>
<table  class="table table-bordered" >
    <thead>
        <tr>
            <th class="col-md-1 align-middle" scope="col">N°</th>
            <th class="col-md-2 align-middle">Nom Prénom</th>
            <th class="col-md-1 align-middle" scope="col">Présence_J</th>
            <th class="col-md-1 align-middle" scope="col">Présence_N</th>
            <th class="col-md-1 align-middle" scope="col">Absence</th>
            <th class="col-md-2 align-middle" scope="col">Heurs Sup</th>

        </tr>
    </thead>
    <tbody>
    @php $i = 0; @endphp

    @foreach($employees as $employee)

        <tr>
            <th colspan="1" width=30> {{$loop->iteration}}</th>
            <td>{{$employee ->nom_employee}} {{$employee ->prenom_employee}}</td>


            <td>{{Form::hidden("pointage_j_$i",0,['id' => "pointage_j_$i"])}}{{ Form::checkbox("pointage_j[]", 1, false,['onclick' => 'this.previousSibling.value=1-this.previousSibling.value'],['id' => "pointage_jj.$i"]) }}</td>

            <td>{{Form::hidden("pointage_n_$i",0,['id' => "pointage_n_$i"])}}{{ Form::checkbox("pointage_n[]", 1, false,['onclick' => 'this.previousSibling.value=1-this.previousSibling.value'],['id' => "pointage_nn.$i"]) }}</td>

            <td>{{Form::hidden("pointage_absence_$i",0,['id' => "pointage_absence_$i"])}}{{ Form::checkbox("pointage_absence[]", 1, false,['onclick' => 'this.previousSibling.value=1-this.previousSibling.value'],['id' => "pointage_absencee.$i"]) }}</td>

            <td>{{Form::text("heur_sup[]", '0', ['class' => 'form-control','id'=>"heur_sup.$i"])}}</td>


            {{Form::hidden("employee_id[]", $employee->id,['employee_id'=>"employee_id.$i"])}}
        </tr>
        @php $i = $i + 1; @endphp
@endforeach
    </tbody>
</table>

<button type="submit" class="btn btn-primary mb-2" >Enregistrer</button>

{!! Form::close() !!}

我的控制器:

    public function store(Request $request)
{
    $this->validate($request,[
        'nbr'=>'required|integer',
        'employee_id.*'=>'required|integer',
        'date_pointage'=>'required|date',
        'pointage_j.*'=>'required|integer|between:0,1',
        'pointage_n.*'=>'required|integer|between:0,1',
        'pointage_absence.*'=>'required|integer|between:0,1',
        'heur_sup.*'=>'nullable|integer|between:0,5',
    ]);

   // dd($request->all());

    $nbr=$request->input('nbr');

    $i=0;
    while ($i < $nbr){

        $pointage = new Pointage;
        $pointage-> employee_id = $request->input("employee_id.$i");
        $pointage-> date_pointage = $request->input("date_pointage");
        $pointage-> jour_pointage = $request->input("pointage_j_$i");
        $pointage-> nuit_pointage = $request->input("pointage_n_$i");
        $pointage-> absence_pointage = $request->input("pointage_absence_$i");
        $pointage-> heurs_sup_pointage = $request->input("heur_sup.$i");
        $pointage-> save();
        $i++;
    }

    return redirect('/pointages')->with('success','Enregistrement reussi');

}
4

0 回答 0