为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
侃侃尔雅 2022-09-25 20:56:14
我有两张桌子 - “旅行”和“活动”。一次旅行可以有许多事件。我有一个表单,允许用户将事件添加到他们的旅行中。选择事件的开始日期和结束日期时,我只想允许用户选择从旅行开始日期和旅行结束日期开始的日期。例如,如果从 3 月 1 日到 3 月 14 日创建了前往纽约的旅行,则用户只能在这些日期之间添加事件。有没有一个好方法可以做到这一点?行程表 事件表添加事件刀片.php '<div class="form-group">    <div class="form-group">        {!! Form::label('trip_id', 'Your Destinations:') !!}        <select class="form-control" name="trip_id">          @foreach($trips as $trip)            <option value='{{ $trip->id}}'>{{$trip->destination}}</option>          @endforeach        </select>      </div>    </div><div class="form-group">  <div class="form-group">    {!! Form::label('event_name', 'Add Event:') !!}    <div class="">      {!! Form::text('event_name', null, ['class' => 'form-control']) !!}      {!! $errors->first('event_name', '<p class="alert alert-danger">:message</p>') !!}    </div>  </div></div><div class="form-group">  <div class="form-group">    {!! Form::label('start_date', 'Start Date:') !!}    <div class="">      {!! Form::date('start_date', \Carbon\Carbon::now(), ['class' => 'form-control']) !!}      {!! $errors->first('start_date', '<p class="alert alert-danger">:message</p>') !!}    </div>  </div></div><div class="form-group">  <div class="form-group">    {!! Form::label('end_date', 'End Date:') !!}    <div class="">      {!! Form::date('end_date', null, ['class' => 'form-control']) !!}      {!! $errors->first('end_date', '<p class="alert alert-danger">:message</p>') !!}    </div>  </div></div><div class="form-group" &nbsp;<br/>{!! Form::submit('Add Event', ['class' => 'btn btn-primary']) !!}`事件控制器public function addEvent(Request $request){  $validator = Validator::make($request->all(),[    'event_name' => 'required',    'start_date' => 'required',    'end_date' => 'required',    'trip_id'=> 'required',  ]);  if($validator->fails()) {    \Session::flash('warning', 'Please enter the valid details');    return redirect('/events')->with('input', Input::all());  }
查看完整描述

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);


查看完整回答
反对 回复 2022-09-25
?
慕田峪7331174

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);

}


查看完整回答
反对 回复 2022-09-25
  • 2 回答
  • 0 关注
  • 138 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信