0
  • Language: PHP
  • Framework: Laravel
  • Dependency / Library: Laravel Voyager

I have 3 Database tables: Users, Schedule of Reviews and Appointments.

The users can reserve many schedules, and schedules can be reserved by many users, so basically, the Appointments table is my associative entity to connect the Users and Schedule of Reviews tables

In my installed Laravel Voyager, the Appointments table has a column status to be marked as Paid / Not yet Paid

Now, Whenever the Admin changes the status to PAID, I want the slots column from the Schedule of Appointments table to be decremented or decreased by 1 to literally mark that the user has successfully paid his/her slot for the schedule of review

May I know how or where can I add such code so when Laravel Voyager updates the Appointments table, I can also update the Schedule of Reviews table?

Thank you very much!

1 Answers1

2

The typical way to achieve this particular scenario is by overriding model, you can implement logic in your Model, which I am assuming App\ScheduleAppointments:

    namespace App;

    use Illuminate\Database\Eloquent\Model; 

    class ScheduleAppointments extends Model 
    {
        //Your model logic
        //
        //
        //
        //

    /**
     * OVERRIDE Boot Method
     */
        protected static function boot()
        {
            parent::boot();
            static::updating(function ($appointment) {
            if($appointment->status == 'PAID'){
                 $appointment->slot -=1;
            }    
            });
        }

    }
Saad Bhutto
  • 594
  • 1
  • 8
  • 22
  • However, for this one, if I'll be updating the details of the Appointment, even not the "status" column, it would make the slots decremented, is there a way for me to specify that the admin has updated the status, then that's the only moment I can decrement the slot? –  Sep 22 '18 at 23:34
  • Yeahh, , you will have the appointment object in the model, Basically, you can check if($appointment->status == 'PAID'){ $appointment->slot -=1;} In that way you will be updating only in the case of status changed – Saad Bhutto Sep 24 '18 at 01:47
  • 1
    Thank you sir Saad! Laravel Events did the work! thank you very much! –  Sep 25 '18 at 09:52