Сначала я извиняюсь за мой английский.
У меня есть форма с вводом сом и флажок с динамическим идентификатором, например 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');
}