在我的Laravel-5.8项目中,我试图向数据库提交员工休假申请。我基本上有两张桌子:
受保护的$fillable =‘假日_名称’,‘假日_日期’,'created_at',;
控制器
public function store(StoreLeaveRequestRequest $request)
{
$commencementDate = Carbon::parse($request->commencement_date);
$resumptionDate = Carbon::parse($request->resumption_date);
$holidays = DB::table('hr_holidays')->select('holiday_date')->whereYear('created_at', '=', date('Y'))->get();
$days = $commencementDate->diffInDaysFiltered(function (Carbon $date) use ($holidays) {
return $date->isWeekday() && !in_array($date, $holidays);
}, $resumptionDate);
$leaverequest = HrLeaveRequest::create([
'leave_type_id' => $request->leave_type_id,
'commencement_date' => $commencementDate,
'resumption_date' => $resumptionDate,
'no_of_days' => $days,
]);
Session::flash('success', 'Leave Request is created successfully');
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()->addDay()->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()->addDay()->format('Y-m-d')}}" max="{{Carbon\Carbon::now()->lastOfYear()->format('Y-m-d')}}">
</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>
</div>
</form>
我希望应用程序通过在no_of_days表中使用resumption_date和commencement_date之间的差异来获得hr_leave_request,并且不包括周末。然后去hr_holiday桌,在假期内享受公共假期。
我用这个做这个:
$days = $commencementDate->diffInDaysFiltered(function (Carbon $date) use ($holidays) {
return $date->isWeekday() && !in_array($date, $holidays);
注意:见控制器中的完整代码。
当我提交时,我得到了以下错误:
"in_array()期望参数2是数组,对象给定“
我该怎么解决呢?
谢谢。
发布于 2020-04-10 10:10:34
返回错误,因为表响应是一个集合。
试着替换这个
$holidays = DB::table('hr_holidays')->select('holiday_date')->whereYear('created_at', '=', date('Y'))->get();
有了这个
$holidays = DB::table('hr_holidays')->select('holiday_date')->whereYear('created_at', '=', date('Y'))->get()->pluck('holiday_date')->toArray();
https://stackoverflow.com/questions/61146046
复制相似问题