Laravel - 避免重复预订时段

Laravel - Avoiding double-booking time slots

我想弄清楚如何添加某种形式的验证,以便同一影院时段不能在同一日期预订两次。非常感谢任何建议。

下面的代码是如何创建预订的,目前用户在表格中有一组选项列表,可以在 TheatreRoomID 字段中选择剧院时间段。使用 Form::Date 输入选择日期。

预订 - Create.blade.php

@extends('layouts.app')
@section('content')
<hr>
<h1>Booking Form</h1>
<hr>
{!! Form::open(['action' => 'BookingFormsController@store', 'method' => 'POST']) !!}

<div class="form-group">
{{Form::label('requestID', 'Request ID')}}
{{Form::number('requestID', $patientDetail->requestID, ['class' => 'form-control',])}}
</div>

<div class="form-group">
{{Form::label('requestDate', 'Request Date')}}
{{Form::date('requestDate', $patientDetail->requestDate, ['class' => 'form-control'])}}
</div>

<div class="form-group">
{{Form::label('patientID', 'Patient ID')}}
{{Form::number('patientID', $patientDetail->patientID, ['class' => 'form-control'])}}
</div>

<div class="form-group">
{{Form::label('patientForename', 'Patient Forename')}}
{{Form::text('patientForename', $patientDetail->patientForename, ['class' => 'form-control'])}}
</div>

<div class="form-group">
{{Form::label('patientSurname', 'Patient Surname')}}
{{Form::text('patientSurname', $patientDetail->patientSurname, ['class' => 'form-control',])}}
</div>

<div class="form-group">
{{Form::label('patientSex', 'Patient Sex')}}
{{Form::text('patientSex', $patientDetail->patientSex, ['class' => 'form-control', 'placeholder' => ''])}}
</div>

<div class="form-group">
{{Form::label('patientDOB', 'Patient DOB')}}
{{Form::date('patientDOB', $patientDetail->patientDOB, ['class' => 'form-control','placeholder' => ''])}}
</div>

<div class="form-group">
{{Form::label('patientUrgency', 'Patient Urgency')}}
{{Form::text('patientUrgency', $patientDetail->patientUrgency, ['class' => 'form-control','placeholder' => ''])}}
</div>

<div class="form-group">
{{Form::label('TheatreRoomID', 'Theatre Room')}}
{{Form::select('TheatreRoomID', ['Room 1 - Time: 9:00AM - 11:00AM' => 'Room 1 - Time: 9:00AM - 11:00AM',
                                'Room 1 - Time: 12:00PM -2:00PM' => 'Room 1 - Time: 12:00PM - 2:00PM',
                                'Room 1 - Time: 3:00PM - 5:00PM' => 'Room 1 - Time: 3:00PM - 5:00PM',
                                'Room 2 - Time: 9:00AM - 11:00AM' => 'Room 2 - Time: 9:00AM - 11AM',
                                'Room 2 - Time: 12:00PM - 2:00PM' => 'Room 2 - Time: 12:00PM - 2:00PM',
                                'Room 2 - Time: 3:00PM - 5:00PM' => 'Room 2 - Time: 3:00PM - 5:00PM',
                                'Room 3 - Time: 9:00AM - 11:00AM' => 'Room 3 - Time: 9:00AM - 11:00AM',
                                'Room 3 - Time: 12:00PM - 2:00PM' => 'Room 3 - Time: 12:00PM - 2:00PM',
                                'Room 3 - Time: 3:00PM - 5:00PM' => 'Room 3 - Time: 3:00PM - 5:00PM'

                                ], null, ['class' => 'form-control','placeholder' => 'Select Theatre Slot'])}}
</div>

<div class="form-group">
{{Form::label('surgeryType', 'Surgery Type')}}
{{Form::text('surgeryType', $patientDetail->surgeryType, ['class' => 'form-control','placeholder' => ''])}}
</div>

<div class="form-group">
  {{Form::label('surgeryDate', 'Surgery Date')}}
  {{Form::date('surgeryDate')}}
</div>

<div class="form-group">
  {{Form::label('performingSurgeon', 'Peforming Surgeon')}}
  {{Form::select('performingSurgeon', ['Wendy clarke' => 'Wendy Clarke', 'John Kennedy' => 'John Kennedy', 'Imran Yousuf' => 'Imran Yousuf', 'Merideth Grey' => 'Merideth Grey', 'Derek Shepherd' => 'Derek Shepherd'], null, ['class' => 'form-control','placeholder' => ''])}}
</div>

<div class="form-group">
{{Form::label('bloodGroup', 'Blood Group')}}
{{Form::select('bloodGroup', ['A' => 'A', 'B' => 'B', 'O' => 'O', 'AB' => 'AB'], null, ['class' => 'form-control','placeholder' => $patientDetail->bloodGroup])}}
</div>

<div class="form-group">
  {{Form::label('patientNotes', 'Patient Notes')}}
  {{Form::textarea('patientNotes', '', ['class' => 'form-control', 'placeholder' => 'Enter any other neccessary patient details'])}}
</div>

<div class="btn-toolbar">
  <a href="javascript:history.back()" class="btn btn-danger mr-3">Back</a>
  {{Form::submit('Submit Booking', ['class'=> 'btn btn-success mr-3'])}}
</div>
{!! Form::close() !!}
@endsection

BookingFormController:

public function create()
{
    return view('bookingforms.create');
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $booking)
{
  $this->validate($booking, [
  'requestID' => 'required',
  'patientID' => 'required',
  'patientForename' => 'required',
  'patientSurname'=> 'required',
  'patientSex' => 'required',
  'patientDOB' => 'required',
  'surgeryType' => 'required',
  'surgeryDate' => 'required',
  'performingSurgeon' => 'required',
  'TheatreRoomID' => 'required',
  'patientUrgency' => 'required',
  'patientNotes' => 'required',
  'bloodGroup' => 'required'
  ]);

  // Create new Booking Form
  $bookingform = new Bookingform;
  $bookingform->requestID = $booking->input('requestID');
  $bookingform->bookingID = $booking->input('bookingID');
  $bookingform->patientID = $booking->input('patientID');
  $bookingform->patientForename = $booking->input('patientForename');
  $bookingform->patientSurname = $booking->input('patientSurname');
  $bookingform->patientSex = $booking->input('patientSex');
  $bookingform->patientDOB = $booking->input('patientDOB');
  $bookingform->surgeryType = $booking->input('surgeryType');
  $bookingform->surgeryDate = $booking->input('surgeryDate');
  $bookingform->performingSurgeon = $booking->input('performingSurgeon');
  $bookingform->TheatreRoomID = $booking->input('TheatreRoomID');
  $bookingform->patientUrgency = $booking->input('patientUrgency');
  $bookingform->patientNotes = $booking->input('patientNotes');
  $bookingform->bloodGroup = $booking->input('bloodGroup');

  //Save Booking form

  $bookingform->save();

  //redirect
  return redirect('/bookingforms')->with('success', 'Booking Submitted');
}

BookingForm.php 型号

    <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class BookingForm extends Model
{
  protected $table = 'bookingforms';
  //primary key
  public $primaryKey = 'bookingID';
  //Timestamps
  public $timestamps = true;
}

预订表格Table结构: see table structure here

我是 Laravel 的新手,非常感谢任何建议。

使用Middleware:

  • 首先你使用这个命令定义一个中间件php artisan make:middleware MiddlewareName

我已经用名称 CheckTheatre
定义了我的
那么 Middleware 所做的就是过滤进入您的应用程序的 HTTP 请求。

这个:

namespace App\Http\Middleware;

use App\BookingForm;
use Closure;

class CheckTheatre
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
       return BookingForm::where([['surgeryDate',$request->input('surgeryDate')],
            ['TheatreRoomID',$request->input('TheatreRoomID')]])->exists() ? 
            redirect('home'):$next($request);
    }
}


所以这个函数的作用是检查是否存在与当前请求 TheatreRoomID 具有相同日期的记录,(我假设你指的日期是surgeryDate)

如果有记录会将他重定向到 home, 但是 你可以 return如果您愿意,可以显示错误消息。

如果没有任何记录,则请求通过并调用 store


现在,在你创建了处理请求的函数之后,你需要告诉中间件要处理哪个请求。
所以在routes.web中可以这样调用中间件:

Route::post('bookingoffer', 'yourController@store')->middleware('MiddlewareName');

并确保将您制作的中间件添加到 app/Http/Kernel.php

建议

阅读 Mass Assignment & Form Request Validation 它将使您的控制器更具可读性,并使编码更容易。