I think you want to use propriety date so change your code to $this->date in the use and the closure.
Aug 10, 2022
16
Level 2
Accessing livewire select data
I need to access select data for my function. But it says that $date is an undefined variable. What exactly I'm doing wrong when I'm trying to access it? My blade:
<select class="custom-select-box" name="date" id="date" wire:model="date">
@foreach ($available_dates as $available_date)
<option value="{{ $available_date }}">{{ $available_date }}</option>
@endforeach
</select>
@error('date')
<div class="text-sm text-red-400">{{ $message }}</div>
@enderror
</div>
Function:
class AppointmentsCreate extends Component
{
public $first_name;
public $last_name;
public $email;
public $phone_number;
public $date;
public $option;
public $comment;
public $time_slot_id;
public $date_input;
public function rules(){
return [
'first_name' => ['required'],
'last_name' => ['required'],
'email' => ['required', 'email'],
'phone_number' => ['required', 'numeric'],
'date' => ['required', 'date', new DateBetween],
'time_slot_id' => ['required'],
'comment' => ['required'],
'option' => ['required'],
];
}
public function render()
{
$times = TimeSlot::where('status', 1)->get();
$date_today = now()->addHours(6);
if ($date_today->isTomorrow()) {
$date_today = now()->endOfDay();
}
$times_today = TimeSlot::where('status', 1)
->whereTime('time', '>', $date_today)
->get();
$available_dates = array();
$appointments_today = Appointment::whereDate('date', Carbon::now())->whereHas('time_slot', function ($query) use($date_today){
$query->where('time', '>', $date_today);
})->get();
if (count($appointments_today) < count($times_today)){
array_push($available_dates, Carbon::now()->format('Y-m-d'));
}
for ($i = 1; $i < 7; $i++) {
$appointments = Appointment::whereDate('date', Carbon::now()->addDays($i))->get();
if (count($appointments) < count($times)){
array_push($available_dates, Carbon::now()->addDays($i)->format('Y-m-d'));
}
}
//a piece of code where i receive an error
$appointment_time_ids = Appointment::orderBy('date')->get()->filter(function($value) use($date){
return $value->date->format('Y-m-d') == $date->format('Y-m-d');
})->pluck('time_slot_id');
if($this->date->format('Y-m-d') == Carbon::now()->format('Y-m-d')){
$time_slots = TimeSlot::where('status', 1)->
whereNotIn('id', $appointment_time_ids)->orderBy('time')
->where('time', '>', $date_today)->get();
} else {
$time_slots = TimeSlot::where('status', 1)->
whereNotIn('id', $appointment_time_ids)->orderBy('time')->get();
}
return view('livewire.appointments-create', compact('available_dates', 'time_slots'));
}
}
Please or to participate in this conversation.