最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Laravel Livewire VOLT, I get undefined variable name error - Stack Overflow

programmeradmin1浏览0评论

I am trying to develop a page in which available appointments are listed. So the students will be able to choose one for foreign language talking exercise. Everything is fine. I get

Undefined variable $showConfirmationModal

error at line @if ($showConfirmationModal) .

I checked the variable names many times. This is because something else I guess. I also made a "find and replace" for $showConfirmationModal. I get the same error message.

I used the artisan commands:

php artisan cache:clear
php artisan view:clear

No help.

<?php

use App\Mail\AppointmentNotificationForStudent;
use App\Mail\AppointmentNotificationForTeacher;
use App\Models\TeacherAvailability;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;
use Livewire\Component;
use Masmerise\Toaster\Toaster;

new class extends Component {
    public $showConfirmationModal = false;
    public $selectedAppointmentId = null;
    public $appointments = [];

    public function mount()
    {
        $this->loadAppointments();
    }

    public function loadAppointments()
    {
        $this->appointments = TeacherAvailability::where('date', '>=', now()->toDateString())
            ->orderBy('date', 'ASC')
            ->orderBy('time_start', 'ASC')
            ->get();
    }

    public function confirmSelectAppointment($appointmentId)
    {
        $this->selectedAppointmentId = $appointmentId;
        $this->showConfirmationModal = true;
    }

    public function selectAppointment()
    {
        $studentId = Auth::id();
        $availability = TeacherAvailability::find($this->selectedAppointmentId);

        if (!$availability) {
            Toaster::error('Appointment not found.');
            return;
        }
 
        $availability->update([
            'student_id'      => $studentId,
            'is_appointment'  => 0,
        ]);

        Toaster::success('Randevu başarıyla seçildi!');
        $this->showConfirmationModal = false;

        $student = Auth::user();
  
        $teacher = $availability->teacher;  
        $teacherEmail = $teacher->email;
        $teacherName  = $teacher->name;

        Mail::to($teacherEmail)->queue(new AppointmentNotificationForTeacher($availability, $teacherEmail, $student, $teacherName));
        Toaster::success('Notification sent to the teacher.');

        
        Mail::to($student->email)->queue(new AppointmentNotificationForStudent($availability, $student));
        Toaster::success('Appointment information sent you in an e-mail.');

        // Populate List
        $this->loadAppointments();
    }
};
?>
<div>
    <!-- I get error at the line below-->
    @if ($showConfirmationModal) 
        <div class="fixed inset-0 bg-black bg-opacity-50 flex justify-center items-center z-50">
            ...
        </div>
    @endif

    <!-- Appointment List -->
    <div>
       ...
       ...
    </div>
</div>

I am trying to develop a page in which available appointments are listed. So the students will be able to choose one for foreign language talking exercise. Everything is fine. I get

Undefined variable $showConfirmationModal

error at line @if ($showConfirmationModal) .

I checked the variable names many times. This is because something else I guess. I also made a "find and replace" for $showConfirmationModal. I get the same error message.

I used the artisan commands:

php artisan cache:clear
php artisan view:clear

No help.

<?php

use App\Mail\AppointmentNotificationForStudent;
use App\Mail\AppointmentNotificationForTeacher;
use App\Models\TeacherAvailability;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;
use Livewire\Component;
use Masmerise\Toaster\Toaster;

new class extends Component {
    public $showConfirmationModal = false;
    public $selectedAppointmentId = null;
    public $appointments = [];

    public function mount()
    {
        $this->loadAppointments();
    }

    public function loadAppointments()
    {
        $this->appointments = TeacherAvailability::where('date', '>=', now()->toDateString())
            ->orderBy('date', 'ASC')
            ->orderBy('time_start', 'ASC')
            ->get();
    }

    public function confirmSelectAppointment($appointmentId)
    {
        $this->selectedAppointmentId = $appointmentId;
        $this->showConfirmationModal = true;
    }

    public function selectAppointment()
    {
        $studentId = Auth::id();
        $availability = TeacherAvailability::find($this->selectedAppointmentId);

        if (!$availability) {
            Toaster::error('Appointment not found.');
            return;
        }
 
        $availability->update([
            'student_id'      => $studentId,
            'is_appointment'  => 0,
        ]);

        Toaster::success('Randevu başarıyla seçildi!');
        $this->showConfirmationModal = false;

        $student = Auth::user();
  
        $teacher = $availability->teacher;  
        $teacherEmail = $teacher->email;
        $teacherName  = $teacher->name;

        Mail::to($teacherEmail)->queue(new AppointmentNotificationForTeacher($availability, $teacherEmail, $student, $teacherName));
        Toaster::success('Notification sent to the teacher.');

        
        Mail::to($student->email)->queue(new AppointmentNotificationForStudent($availability, $student));
        Toaster::success('Appointment information sent you in an e-mail.');

        // Populate List
        $this->loadAppointments();
    }
};
?>
<div>
    <!-- I get error at the line below-->
    @if ($showConfirmationModal) 
        <div class="fixed inset-0 bg-black bg-opacity-50 flex justify-center items-center z-50">
            ...
        </div>
    @endif

    <!-- Appointment List -->
    <div>
       ...
       ...
    </div>
</div>
Share Improve this question edited 19 hours ago zkanoca asked 19 hours ago zkanocazkanoca 9,91810 gold badges52 silver badges98 bronze badges 3
  • $showConfirmationModal is not a variable. It's a property of class Component. – KIKO Software Commented 19 hours ago
  • @KIKOSoftware so? Fine, they are property. However Laravel says "Undefined variable $showConfirmationModal". – zkanoca Commented 19 hours ago
  • 1 The error simply means the variable doesn't exist, which is true. To get the property you first need to instantiate an object of class Component, like so: $myComponent = new Component();. Then you might want to use one of its methods before you access the property like so: if ($myComponent->showConfirmationModal) – KIKO Software Commented 18 hours ago
Add a comment  | 

1 Answer 1

Reset to default 0

Finally I noticed that I have called the wrong "Component" Class.

use Livewire\Component;

should be

use Livewire\Volt\Component;

Now, everything is fine.

发布评论

评论列表(0)

  1. 暂无评论