В моем Laravel -5.8 Выйти из проекта у меня есть этот класс модели:
protected $table = 'hr_leave_requests';
protected $fillable = [
'id',
'reason',
'leave_type_id',
'commencement_date',
'resumption_date',
'no_of_days',
'relief_officer_id',
];
Уже я могу успешно сохранить данные в базе данных контроллера
public function store(StoreLeaveRequestRequest $request)
{
try {
$commencementDate = Carbon::parse($request->commencement_date);
$resumptionDate = Carbon::parse($request->resumption_date);
$leaverequest = HrLeaveRequest::create([
'leave_type_id' => $request->leave_type_id,
'commencement_date' => $commencementDate,
'resumption_date' => $resumptionDate,
'reason' => $request->reason,
]);
Session::flash('success', 'Leave Request is created successfully');
return redirect()->route('service.leave_requests.index');
}
catch (Exception $exception)
{
Session::flash('error', 'Action failed!');
return redirect()->route('service.leave_requests.index');
}
}
Просмотр
<form action="{{route('service.leave_requests.store')}}" method="post" class="form-horizontal" enctype="multipart/form-data">
{{csrf_field()}}
<div class="card-body">
<div class="form-body">
<div class="row">
<div class="col-sm-4">
<div class="form-group">
<label>Leave Type:<span style="color:red;">*</span></label>
<select class="form-control select2bs4" data-placeholder="Choose Leave Type" tabindex="1" name="leave_type_id" style="width: 100%;">>
<option value="">Select Leave Type</option>
@if($leavetypes->count() > 0)
@foreach($leavetypes as $leavetype)
<option value="{{$leavetype->id}}">{{$leavetype->leave_type_name}}</option>
@endforeach
@endif
</select>
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label>Commencement Date:<span style="color:red;">*</span></label>
<input type="date" class="form-control" placeholder="dd/mm/yyyy" name="commencement_date" value="{{old('commencement_date')}}" min="{{Carbon\Carbon::now()->firstOfYear()->format('Y-m-d')}}" max="{{Carbon\Carbon::now()->lastOfYear()->format('Y-m-d')}}">
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label>Resumption Date:<span style="color:red;">*</span></label>
<input type="date" class="form-control" placeholder="dd/mm/yyyy" name="resumption_date" value="{{old('resumption_date')}}" min="{{Carbon\Carbon::now()->firstOfYear()->format('Y-m-d')}}" max="{{Carbon\Carbon::now()->lastOfYear()->format('Y-m-d')}}">
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label>Leave Days:<span style="color:red;">*</span></label>
<input type="number" name="no_of_days" class="form-control" value="{{old('no_of_days')}}" style="width: 100%;" disabled>
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<label>Reason</label>
<textarea rows="2" name="reason" class="form-control" placeholder="Enter Reason here" value="{{old('reason')}}"></textarea>
</div>
</div>
</div>
</div>
</div>
<!-- /.card-body -->
<div class="card-footer">
<button type="submit" id="submit_create" class="btn btn-primary">{{ trans('global.save') }}</button>
<button type="button" onclick="window.location.href='{{route('service.leave_requests.index')}}'" class="btn btn-default">Cancel</button>
</div>
</form>
<script type="text/javascript">
$(document).ready(function() {
$(function () {
$('#commencement_date').datetimepicker({
format: "YYYY-MM-DD"
});
$('#resumption_date').datetimepicker({
format: "YYYY-MM-DD"
});
});
});
</script>
Чего я хочу добиться, так это того, чтобы при замене resuming_date вычислялась разница между commencement_date и resuming_date, а результат загружался как целое число в no_of_days.
Как мне этого добиться?
Спасибо.