admin管理员组

文章数量:1123341

I am trying to list the available online lesson records. I use Laravel 11, LiveWire with VOLT Class stack. I get Undefined variable $appointments error message at line @if ($appointments->isEmpty())

<?php

use Illuminate\Support\Facades\Auth;
use Livewire\Volt\Component;

new class extends Component {
    public $appointments;

    public function mount()
    {
        $this->appointments = $this->getAppointments();
        $this->getAppointments();
        //edited here after the first answer
    }

    public function getAppointments()
    {
   
    return Appointment::where('deleted', 0)->whereRaw('max_student > (SELECT COUNT(*) FROM appointment_students WHERE appointment_students.appointmentid = appointments.id)')->get();
    }

    public function selectAppointment($appointmentId)
    {
        $studentId = Auth::id();  
        $appointment = Appointment::find($appointmentId);

        if (!$appointment) {
             session()->flash('error', 'Appointment not found.');
             return;
        }

     
        if ($appointment->students()->where('studentid', $studentId)->exists()) {
            session()->flash('error', 'You have already selected this appointment.');
            return;
        }

   
        $appointment->students()->attach($studentId);

        session()->flash('success', 'Appointment selected successfully!');
        $this->mount();  
    }
}; ?>

<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight">
            {{ __('Online Lessons') }}
        </h2>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6 text-gray-900 dark:text-gray-100">
                    <div class="grid grid-cols-3 gap-4">  
                        @if ($appointments->isEmpty())
                            <p>{{ __('No available appointments.') }}</p>
                        @else
                            @foreach ($appointments as $appointment)
                                <div class="border p-4 rounded text-center">
                                    <p><strong>{{ $appointment->date }}</strong></p>
                                    <p>{{ $appointment->time_start }} - {{ $appointment->time_end }}</p>
                                    <p>{{ __('Capacity') }}:
                                        {{ $appointment->students()->count() }}/{{ $appointment->max_student }}</p>
                                    <button wire:click="selectAppointment({{ $appointment->id }})" class="bg-blue-500 text-white px-4 py-2 rounded">
                                        {{ __('Select') }}
                                    </button>
                                </div>
                            @endforeach
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>

I have updated the mount() method as follows:

public function mount()
{
    $this->appointments = $this->getAppointments();
    dd($this->appointments); //dd() isn't invoked.
}

I have noticed that mount() method is never called.

I am trying to list the available online lesson records. I use Laravel 11, LiveWire with VOLT Class stack. I get Undefined variable $appointments error message at line @if ($appointments->isEmpty())

<?php

use Illuminate\Support\Facades\Auth;
use Livewire\Volt\Component;

new class extends Component {
    public $appointments;

    public function mount()
    {
        $this->appointments = $this->getAppointments();
        $this->getAppointments();
        //edited here after the first answer
    }

    public function getAppointments()
    {
   
    return Appointment::where('deleted', 0)->whereRaw('max_student > (SELECT COUNT(*) FROM appointment_students WHERE appointment_students.appointmentid = appointments.id)')->get();
    }

    public function selectAppointment($appointmentId)
    {
        $studentId = Auth::id();  
        $appointment = Appointment::find($appointmentId);

        if (!$appointment) {
             session()->flash('error', 'Appointment not found.');
             return;
        }

     
        if ($appointment->students()->where('studentid', $studentId)->exists()) {
            session()->flash('error', 'You have already selected this appointment.');
            return;
        }

   
        $appointment->students()->attach($studentId);

        session()->flash('success', 'Appointment selected successfully!');
        $this->mount();  
    }
}; ?>

<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight">
            {{ __('Online Lessons') }}
        </h2>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6 text-gray-900 dark:text-gray-100">
                    <div class="grid grid-cols-3 gap-4">  
                        @if ($appointments->isEmpty())
                            <p>{{ __('No available appointments.') }}</p>
                        @else
                            @foreach ($appointments as $appointment)
                                <div class="border p-4 rounded text-center">
                                    <p><strong>{{ $appointment->date }}</strong></p>
                                    <p>{{ $appointment->time_start }} - {{ $appointment->time_end }}</p>
                                    <p>{{ __('Capacity') }}:
                                        {{ $appointment->students()->count() }}/{{ $appointment->max_student }}</p>
                                    <button wire:click="selectAppointment({{ $appointment->id }})" class="bg-blue-500 text-white px-4 py-2 rounded">
                                        {{ __('Select') }}
                                    </button>
                                </div>
                            @endforeach
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>

I have updated the mount() method as follows:

public function mount()
{
    $this->appointments = $this->getAppointments();
    dd($this->appointments); //dd() isn't invoked.
}

I have noticed that mount() method is never called.

Share Improve this question edited 6 hours ago zkanoca asked 14 hours ago zkanocazkanoca 9,91810 gold badges52 silver badges98 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You forgot to set it in your mount method.

public function mount(): void
{
    $this->appointments = $this->getAppointments();
}

本文标签: laravelLivewire VoltHow to list current recordsStack Overflow