Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ilya_user's avatar

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'));
    }


}


0 likes
16 replies
OussamaMater's avatar

I think you want to use propriety date so change your code to $this->date in the use and the closure.

Ilya_user's avatar

@OussamaMater seems like it's working now thanks. But now I receive this error: Call to a member function format() on null . So seems like it's not retrieving the input data from the form select date.

OussamaMater's avatar

@Ilya_user try dd($this->date) and see what it gives you, because I think the error is in this line $value->date->format('Y-m-d') , are you sure the field name is correct and is not null?

Edit, I think some records in the table has a null value that's why you're getting that error.

Ilya_user's avatar

@OussamaMater it returns null, $value->date->format('Y-m-d') searches for date column to compare it with the selected date. This function worked perfectly before when I used a two-step form. I saved the data in the first step in the session and compared it with the date in the database in the second step. But since I want to use everything in one form now with the livewire I have to change the code of that function.

OussamaMater's avatar

@Ilya_user okay, but are you sure that all the records you are fetching from the database have a value different than null? and I see no issue in the data binding in Livewire.

Again double check all the records, so we know the format() being called on null is caused by $this->date and not the $value.

Ilya_user's avatar

@OussamaMater dd($this->date) returns null so something wrong with data binding then. $value only has dates, no null

OussamaMater's avatar

@Ilya_user do you have any debugging tool, like debugbar, to see if the data changes when you select something different? are there any requests made or errors in the network tab? because the binding looks correct to me.

Snapey's avatar

@OussamaMater Debugbar is a good call. It has a Livewire section so you can see the state of public properties in the component after every request

1 like
Ilya_user's avatar

@OussamaMater I just downloaded debugbar, and livewire section is empty. Or I should take a look at the other section?

OussamaMater's avatar

@Ilya_user can you take a screenshot and link it to the discussion? it should not be empty, you have different property, so if you're Livewire is loaded properly as you said, you need to see the component name and different attributes and details.

Snapey's avatar

maybe you are not actually loading livewire?

Ilya_user's avatar

@Snapey but I do, when I comment out the section that creates troubles for me it works and livewire section has some data. But when I use that code with time slots it stops showing anything

Ilya_user's avatar

@Snapey I showed images in another reply. Script just stops working when it meets that error with time slots. The problem as I see it is that date from the input is null by default. But for my code to work, it should get the first option from select by the default and then change it if a user gonna choose another option. Is there a way to implement that?

Please or to participate in this conversation.