2 回答
TA贡献1776条经验 获得超12个赞
$start_date = Carbon::parse($request['start_date'])->formet('Y-m-d');
$end_date = Carbon::parse($request['end_date'])->formet('Y-m-d');
$tripCheck = Trip:where('id', $request['trip_id'])
->whereDate('start_date', '>=', $start_dat)
->whereDate('end_date', '<', $end_date)
->first();
$trips = Trip::all();
$status = failed;
$msg = 'The new event has been not added to your trip';
if($tripCheck){
$events = new Events;
$events->event_name = $request['event_name'];
$events->start_date = $request['start_date'];
$events->end_date = $request['end_date'];
$events->trip_id = $request['trip_id'];
$events->save();
$status = 'success';
$msg = 'The new event has been not added to your trip';
}
return redirect('trips')->with($status, $msg)->with('trips', $trips);
TA贡献1828条经验 获得超13个赞
在 addEvent.blade.php文件中,可以根据现有旅行日期将“最小”和“最大”属性添加到start_date和end_date
{!! Form::date('start_date', \Carbon\Carbon::now(), ['class' => 'form-control', 'min' => $trip->startdate, 'max' => $trip->enddate]) !!}
{!! Form::date('end_date', null, ['class' => 'form-control', 'min' => $trip->startdate, 'max' => $trip->enddate]) !!}
在 addEvent 函数中,您必须添加start_date和end_date验证,以便它们不超过行程表的日期
public function addEvent(Request $request)
{
// first take data from the trip table
$trip = Trip::find($request->trip_id);
// if trip not found then error
if( !$trip ){
return redirect('trips')->with('fail', 'Trip not found');
}
// add validations for start_date and end_date so that they do not exceed the dates of the trip table
// gte is for grather than equal
// lte is for less than equal
// or you can use min max
$validator = Validator::make($request->all(),[
'event_name' => 'required',
'start_date' => 'required|date|min:'.$trip->startdate.'|max:'.$trip->enddate, //
'end_date' => 'required|date|gte:start_date|max:'.$trip->enddate,
'trip_id'=> 'required',
]);
if($validator->fails()) {
\Session::flash('warning', 'Please enter the valid details');
return redirect('/events')->with('input', Input::all());
}
$events = new Events;
$trips = Trip::all();
$events->event_name = $request['event_name'];
$events->start_date = $request['start_date'];
$events->end_date = $request['end_date'];
$events->trip_id = $request['trip_id'];
$events->save();
return redirect('trips')->with('success', 'The new event has been added to your trip')->with('trips', $trips);
}
- 2 回答
- 0 关注
- 138 浏览
添加回答
举报