Php Laravel-表单选择日期-仅允许用户在从数据库发送的两个日期之间进行选择

Php Laravel-表单选择日期-仅允许用户在从数据库发送的两个日期之间进行选择,php,laravel,datetime,Php,Laravel,Datetime,我有两张桌子——‘Trips’和‘Events’。一次旅行可以有很多活动。 我有一个表单,允许用户将事件添加到他们的旅行中。 在选择事件的开始日期和结束日期时,我只想允许用户从行程开始日期和行程结束日期中选择一个日期 例如,如果从3月1日到3月14日创建了纽约之旅,则用户只能在这些日期之间添加事件。有什么好办法吗 addEvent.blade.php ` trip.blade.php <div class="form-group"> <input type="t

我有两张桌子——‘Trips’和‘Events’。一次旅行可以有很多活动。 我有一个表单,允许用户将事件添加到他们的旅行中。 在选择事件的开始日期和结束日期时,我只想允许用户从行程开始日期和行程结束日期中选择一个日期

例如,如果从3月1日到3月14日创建了纽约之旅,则用户只能在这些日期之间添加事件。有什么好办法吗

addEvent.blade.php `

trip.blade.php

<div class="form-group">
      <input type="text" name="destination" class="form-control" value="{{$trip->destination}}" placeholder="Destination" />
    </div>
    <h7>Trip Start Date: </h7>
    <div class="form-group">
      <input type="date" name="startdate" class="form-control" value="{{$trip->startdate}}" placeholder="Start Date" />
    </div>
    <h7>Trip End Date: </h7>
    <div class="form-group">
      <input type="date" name="enddate" class="form-control" value="{{$trip->enddate}}" placeholder="End Date" />
    </div>
    <div>

在addEvent.blade.php文件中,可以根据现有行程日期将“min”和“max”属性添加到开始日期和结束日期

{!! 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函数中,您必须为开始日期和结束日期添加验证,以便它们不会超过行程表的日期

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);
}
$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\u id'])
->whereDate('start_date','>=',$start_dat)

->whereDate('end_date'),如果我在编辑功能中添加类似的内容,会有所不同吗?
public function submit(Request $request){
  $this->validate($request, [
    'name'          => 'required',
    'email'         => 'required',
    'destination'   => 'required',
    'startdate'     => 'required',
    'enddate'       => 'required'
  ]);

  //Create new trips
  $trip = new Trip;
  $trip->name = $request->input('name');
  $trip->email = $request->input('email');
  $trip->destination = $request->input('destination');
  $trip->startdate = $request->input('startdate');
  $trip->enddate = $request->input('enddate');
  $trip->user_id = auth()->user()->id;

  //save trips
  $trip->save();

  //Rredirect
  return redirect('/home')->with('status', 'Trip Created Sucessfully');

}
{!! 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]) !!}
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);
}
$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);