【发布时间】: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。仅将图像用于无法表达为文本或增强文本的内容。在图片中包含图例/键和说明。