I am using laravel newbie and I have the following issues. I am using laravel 5.8 and would like to submit a form array to the same table at once. I have a form named timesheets looping through a dates array. id like to collect the hours for each date and save them simultaneously as shown in the image below. https://imgur.com/gNdJVNE
below is my form and controller
TimesheetController.php
public function store(Request $request, Timesheet $timesheet, Analysis $analysis, Activity $activity)
{
$validatedData = $request->validate([
'date_id' => 'required',
'analysis_id' => 'required',
'activity_id' => 'max:1000',
'hour' => 'required|max:100',
'activity_id1' => 'max:1000',
'hour1' => 'max:100',
'activity_id2' => 'max:1000',
'hour2' => 'max:100',
'activity_id3' => 'max:1000',
'hour3' => 'max:100',
'activity_id4' => 'max:1000',
'hour4' => 'max:100',
'activity_id5' => 'max:1000',
'hour5' => 'max:100',
'total_hours' => 'required|max:100',
'approved' => 'required',
'approved_by' => 'required'
]);
$input = Timesheet::all();
$insert=array();
foreach ($input['date_id'] as $key => $date_id) {
$insert[$key]['date_id'] = $date_id;
}
foreach ($input['analysis_id'] as $key => $analysis_id) {
$insert[$key]['analysis_id'] = $analysis_id;
}
foreach ($input['activity_id'] as $key => $activity_id) {
$insert[$key]['activity_id'] = $activity_id;
}
foreach ($input['hour'] as $key => $hour) {
$insert[$key]['hour'] = $hour;
}
foreach ($input['activity_id1'] as $key => $activity_id1) {
$insert[$key]['activity_id1'] = $activity_id1;
}
foreach ($input['hour1'] as $key => $hour1) {
$insert[$key]['hour1'] = $hour1;
}
foreach ($input['activity_id2'] as $key => $activity_id2) {
$insert[$key]['activity_id2'] = $activity_id2;
}
foreach ($input['hour2'] as $key => $hour2) {
$insert[$key]['hour2'] = $hour2;
}
foreach ($input['activity_id3'] as $key => $activity_id3) {
$insert[$key]['activity_id3'] = $activity_id3;
}
foreach ($input['hour3'] as $key => $hour3) {
$insert[$key]['hour3'] = $hour3;
}
foreach ($input['activity_id4'] as $key => $activity_id4) {
$insert[$key]['activity_id4'] = $activity_id4;
}
foreach ($input['hour4'] as $key => $hour4) {
$insert[$key]['hour4'] = $hour4;
}
foreach ($input['activity_id5'] as $key => $activity_id5) {
$insert[$key]['activity_id5'] = $activity_id5;
}
foreach ($input['hour5'] as $key => $hour5) {
$insert[$key]['hour5'] = $hour5;
}
foreach ($input['total_hours'] as $key => $total_hours) {
$insert[$key]['total_hours'] = $total_hours;
}
foreach ($input['approved'] as $key => $approved) {
$insert[$key]['approved'] = $approved;
}
foreach ($input['approved_by'] as $key => $approved_by) {
$insert[$key]['approved_by'] = $approved_by;
}
Timesheet::insert($insert);
return redirect()->route('timesheets.index')->with('success', "Activity Created ");
}
<form action="" method="post" id="create_analysis">
@csrf
@foreach($dates as $date)
<div class="row border" style="padding:3px;" id="timesheet">
<strong></strong>
<input type="hidden" id="date_id" name="date_id[]" value="">
<input type="hidden" id="analysis_id" name="analysis_id[]" value="">
<div class="col-md-1">
<a href="#" data-toggle="tooltip" title="">
</a>
<input type="hidden" id="activity_id" name="activity_id[]" value="">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-clock-o"></i></span>
</div>
<input type="text" name="hour[]" value="0" id="hour" v-model.number="hour" class="form-control ">
</div>
@if ($errors->has('hour'))
<div class="invalid-feedback">
<strong>
</strong>
</div>
@endif
</div>
@if(empty($analysis->act1))
@else
<div class="row col-md-1">
<a href="#" data-toggle="tooltip" title="">
</a>
<input type="hidden" id="activity_id1" name="activity_id1[]" value="">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-clock-o"></i></span>
</div>
<input type="text" name="hour1[]" value="0" id="hour1" v-model.number="hour1" class="form-control ">
@if ($errors->has('hour1'))
<div class="invalid-feedback">
<strong>
</strong>
</div>
@endif
</div>
</div>
@endif
@if(empty($analysis->act2))
@else
<div class="row col-md-1">
<a href="#" data-toggle="tooltip" title="">
</a>
<input type="hidden" id="activity_id2" name="activity_id2[]" value="">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-clock-o"></i></span>
</div>
<input type="text" name="hour2[]" value="0" id="hour2" v-model.number="hour2" class="form-control ">
@if ($errors->has('hour2'))
<div class="invalid-feedback">
<strong>
</strong>
</div>
@endif
</div>
</div>
@endif
@if(empty($analysis->act3))
@else
<div class="row col-md-1">
<a href="#" data-toggle="tooltip" title="">
</a>
<input type="hidden" id="activity_id3" name="activity_id3[]" value="">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-clock-o"></i></span>
</div>
<input type="text" name="hour3[]" value="0" id="hour3" v-model.number="hour3" class="form-control ">
@if ($errors->has('hour3'))
<div class="invalid-feedback">
<strong>
</strong>
</div>
@endif
</div>
</div>
@endif
@if(empty($analysis->act4))
@else
<div class="row col-md-1">
<a href="#" data-toggle="tooltip" title="">
</a>
<input type="hidden" id="activity_id4" name="activity_id4[]" value="">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-clock-o"></i></span>
</div>
<input type="text" name="hour4[]" value="0" id="hour4" v-model.number="hour4" class="form-control ">
@if ($errors->has('hour4'))
<div class="invalid-feedback">
<strong>
</strong>
</div>
@endif
</div>
</div>
@endif
@if(empty($analysis->act5))
@else
<div class="row col-md-1">
<a href="#" data-toggle="tooltip" title="">
</a>
<input type="hidden" id="activity_id5" name="activity_id5[]" value="">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-clock-o"></i></span>
</div>
<input type="text" name="hour5[]" value="0" id="hour5" v-model.number="hour5" class="form-control ">
@if ($errors->has('hour5'))
<div class="invalid-feedback">
<strong>
</strong>
</div>
@endif
</div>
</div>
@endif
<div class="col-md-3">
Total Hours
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-list-ol"></i></span>
</div>
<input type="text" name="total_hours[]" :value="TotalTimesheets" id="total_hours" class="form-control " max="9" >
@if ($errors->has('total_hours'))
<div class="invalid-feedback">
<strong>
</strong>
</div>
@endif
</div>
</div>
<input type="hidden" id="approved" name="approved[]" value="No">
<input type="hidden" id="approved_by" name="approved_by[]" value="">
</div>
@endforeach
<br />
<br />
<div class="form-group">
<button type="submit" class="btn btn-primary btn-lg"></button>
</div>
</form>
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire