【问题标题】:Laravel - Avoiding double-booking time slotsLaravel - 避免重复预订时间段
【发布时间】:2020-04-19 13:40:58
【问题描述】:

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

下面的代码是如何创建预订的,目前用户在表单中有一组选项,可以在 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;
}

BookingForms 表结构: see table structure here

我对 Laravel 还很陌生,因此非常感谢任何建议。

【问题讨论】:

  • 首先,您可能希望在数据库级别设置一些约束。我们需要知道您是如何存储数据的,因此您需要向我们展示相关信息:模型代码和表架构。
  • 您好,我已更新问题以包括模型代码和表结构:)
  • 使用unique 哪个字段是独一无二的,像这样`'home_id' => ['required', 'string', 'unique:homes'],` 其中homes 是一个模型
  • 您如何在数据库级别定义每个房间的可用时间?这就是这个的关键
  • use text, not images/links, for text--including tables & ERDs。仅将图像用于无法表达为文本或增强文本的内容。在图片中包含图例/键和说明。

标签: php laravel laravel-6


【解决方案1】:

使用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但是如果你愿意,你可以返回一条错误消息。

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


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

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

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

建议

阅读Mass AssignmentForm Request Validation,它将使您的控制器更具可读性,并且总体上使编码更容易。

【讨论】:

  • 非常感谢!我现在不在我的电脑旁,但我一回来就会试一试,让你知道我的进展情况!
  • 愚蠢的问题,我要不要将此添加到我的预订表格控制器中?
  • 即使这会起作用,OP 可能想告诉用户发生了什么,所以 Laravel 验证是一个更好的方法。 OP,如果你走这条路,你可能想在文档中阅读有关中间件的信息。
  • 有没有办法使用 Laravel 验证来实现这一点?或者中间件是最直接的方法?
  • @AndrewGlenny 不,您不需要将其添加到BookingForm,创建的中间件将在App/Http/Middleware 文件夹中。关于 Laravel 验证,我认为您不能同时验证 2 个值,但可以通过自定义 validation rules 进行验证,但这会使事情变得过于复杂。
猜你喜欢
  • 2018-08-18
  • 1970-01-01
  • 1970-01-01
  • 2012-08-16
  • 1970-01-01
  • 2013-09-05
  • 1970-01-01
  • 2017-05-15
  • 2023-03-23
相关资源
最近更新 更多