檢查所選日期的可用時間段
我目前正在使用 Livewire 構建一個調度平臺。
什么是現有的?一旦從我的客戶那里獲得包裹,客戶就會收到一條短信和他們的取件 ID 的電子郵件。但是,這會導致我客戶辦公室的人群過多,因為大多數人(如果不是全部的話)幾乎都會出現,如果不是同時出現的話。因此,我的客戶希望我構建一個日程安排應用程式,通過 SMS 和電子郵件警報,要求客戶在來之前預訂日程安排。
我最初做了什么?
- 創建了兩個資料庫:Schedule 和 Timeslots --> Schedules 包含pickup_id、location、scheduled_at、timeslot_id --> Timeslots 包含名稱、limit
- 當客戶選擇日期和時間段時,會在后端運行一個查詢,以根據該時間段的限制計算該特定時間段的計劃數量
$schedules = Schedule::where([
['scheduled_at', $this->scheduled_at],
['timeslot_id', $this->timeslot_id]
])->get();
$timeslot = Timeslot::where('id', $this->timeslot_id)->first();
if (count($schedules) >= $timeslot->limit) {
session()->flash('callout', ['type' => 'error', 'message' => 'Pickup Date is full for that time slot! Kindly choose another date or slot!']);
}
- 如果計劃計數小于時隙限制,請繼續。否則,回傳時間段已滿的警報,選擇另一個日期或時間段。
到了這個階段,輕而易舉。
但是,我的客戶想要的是,只要客戶選擇日期,時間段的選擇下拉選單就應該只顯示可用的時間段。我想做的是:
- 選擇日期
- 后臺查詢獲取當天的所有日程
- 獲取當天所有時間表的所有時間段
- 對于每個時間段,根據該時間段的限制查詢該時間段的計劃計數。
- 如果計劃計數小于限制,則顯示時間段。否則,不顯示時間段
這是我腦海中的想法,但是,我不知道如何將其表示為代碼。這個想法是否正確?我是否需要為此創建一個資料透視表,或者有沒有辦法可以撰寫構建器/雄辯的方法來執行此操作而無需創建任何資料透視表?我在這里需要你的幫助。
計劃刀片如下所示:
<div class="col-md-6 mb-3" wire:ignore.self>
<label class="form-label-lg required">Pickup Date</label>
<div class="input-group">
<input type="text" id="datepicker01" class="datepicker form-control form-control-lg @error('selectedScheduledAt') is-invalid @enderror" placeholder="yyyy/mm/dd" autocomplete="off" data-provide="datepicker" data-date-autoclose="true" data-date-format="yyyy/mm/dd" data-date-today-highlight="true" data-date-start-date="new Date();" data-date-week-start="1" data-date-days-of-week-disabled="0,6" onchange="this.dispatchEvent(new InputEvent('input'))" wire:model="selectedScheduledAt" required>
<label for="datepicker01" class="input-group-text pointer text-white bg-touch"><i class="bi-calendar3"></i></label>
</div>
@error('selectedScheduledAt') <span class="error">{{ $message }}</span> @enderror
</div>
<div class="col-md-6 mb-3">
<label class="form-label-lg required">Time Slot <em class="font-sm">(Choose Pickup Date)</em></label>
<select class="form-select form-select-lg @error('slot_id') is-invalid @enderror" wire:model.defer="slot_id" required>
<option value="">{{ __('Select Time Slot') }}</option>
@if (!empty($timeslots))
@foreach($timeslots as $timeslot)
<option value="{{ $timeslot->id }}">{{ $timeslot->name }}</option>
@endforeach
@endif
</select>
@error('timeslot_id') <span class="error">{{ $message }}</span> @enderror
</div>
我的 Livewire 組件如下所示:
public $selectedScheduledAt = NULL;
public $timeslots = NULL;
public function updatedSelectedScheduledAt($scheduled_at)
{
// this is where I am to get the available $this->timeslots to display but I am hooked.
}
uj5u.com熱心網友回復:
在您的時隙模型中,在下面添加此代碼
public function schedules() {
return $this->hasMany(Schedule::class, 'timeslot_id', 'id');
}
在您的 Livewire 組件中,在下面添加此代碼
public $timeslots = [];
public function updatedSelectedScheduledAt($scheduled_at) {
$timeslots = Timeslot::with('schedules')->get();
foreach ($timeslots as $key => $timeslot) {
$allocated = collect($timeslot->schedules)->where('scheduled_at', $scheduled_at)->where('timeslot_id', $timeslot->id)->count();
if ($allocated < $timeslot->limit) {
$this->timeslots[] = $timeslot;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/527105.html
上一篇:什么會阻止本地化正常作業?
