Menu

Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

Appointment Reminders with PHP and Laravel

Download the Code

This Laravel 5 web application shows how to create appointments for customers at a date in future, and send out reminders for those appointments in a background job that runs every few minutes.

In this tutorial, we'll point out the key bits of code that make this application work. Check out the project README on GitHub to see how to run the code yourself.

Check out how Yelp uses SMS to confirm restaurant reservations for diners.

Let's get started! Click the button below to begin.

Let's get started!

Configure the application to use Twilio

Before we can use the Twilio API to send reminder text messages, we need to configure our account credentials. These can be found on your Twilio Console. You'll also need an SMS-enabled phone number - you can find or purchase a new one to use here.

We configure these values using Dotenv, the configuration library of choice for Laravel applications. More information on how to configure this application can be found in the project README.

Loading Code Sample...
      
      
          
          
          
          
        
      .env.example

      Next let's see how we create a new Appointment.

      Let's create some appointments

      Create a new appointment

      In order to send an appointment reminder, we first need to create an appointment! In the controller, we take information submitted in a form (notably a customer's name and phone number, plus a time for the appointment in the future) and save it in an Appointment model.

      Notice that we use the Carbon date library to make it easier for us to parse and do simple operations with the time.

      Loading Code Sample...
          
          
              
              
              
              
            
          app/Http/Controllers/AppointmentController.php

          Create a new appointment

          app/Http/Controllers/AppointmentController.php

          Now that we have our Appointment created, let's see how to schedule a reminder for it.

          Schedule a job to send reminders

          Schedule a job to send reminders

          Every ten minutes, we'd like our application to check the appointments database to see if any appointments are coming up that require reminders to be sent out. We configure both the job code we'd like to run and the interval on which to run it here.

          Loading Code Sample...
              
              
                  
                  
                  
                  
                
              app/Console/Kernel.php

              Schedule a job to send reminders

              app/Console/Kernel.php

              With our job configured, we're now ready to write the actual console command code that will send out our reminders.

              Create a console command to run the job

              Create a console command to run the job

              To actually execute our recurring job logic, we create an Artisan console command which queries the database for upcoming appointments and sends reminders as necessary. As an added bonus, defining our job logic in this way allows us to run the reminder job whenever we want from the command line.

              Loading Code Sample...
                  
                  
                      
                      
                      
                      
                    
                  app/Console/Commands/SendReminders.php

                  Create a console command to run the job

                  app/Console/Commands/SendReminders.php

                  Let's dig further into the ApplicationReminder class

                  Find appointments that need reminders

                  Find appointments that need reminders

                  Our recurring job uses an instance of the AppointmentReminder class to query the database for appointments coming up in the next ten minutes and send out reminder messages.

                  In the constructor, we execute the database query using a custom scope on the Appointment model. This should give us a list of all appointments with a date and time that falls within the next ten minutes.

                  Loading Code Sample...
                      
                      
                          
                          
                          
                          
                        
                      app/AppointmentReminders/AppointmentReminder.php

                      Find appointments that need reminders

                      app/AppointmentReminders/AppointmentReminder.php

                      Now let's setup the Twilio REST Client in order to send some SMS reminder messages.

                      First, we need a Twilio API client

                      Set up a Twilio API client

                      Also in the AppointmentReminder constructor, we create a Twilio REST Client to send out reminders via SMS. We initialize it with the Twilio account credentials we configured earlier.

                      Loading Code Sample...
                          
                          
                              
                              
                              
                              
                            
                          app/AppointmentReminders/AppointmentReminder.php

                          Set up a Twilio API client

                          app/AppointmentReminders/AppointmentReminder.php

                          With the client and the reminders in hand. All that is left is to send an SMS for them.

                          Send reminder messages with the Twilio API

                          Send reminder messages with the Twilio API

                          These two private functions are called for every appointment coming up that requires a reminder to be sent. The first formats the text of the message to be sent out. The second actually uses the Twilio REST API client to send out a text message.

                          We provide a to parameter which is the customer's phone number, a from parameter which is a number in our account, and a body parameter which contains the text of the message.

                          Loading Code Sample...
                              
                              
                                  
                                  
                                  
                                  
                                
                              app/AppointmentReminders/AppointmentReminder.php

                              Send reminder messages with the Twilio API

                              app/AppointmentReminders/AppointmentReminder.php

                              That's it! Our Laravel application is all set to send out reminders for upcoming appointments.

                              Where to next?

                              Where to next?

                              We hope you found this sample application useful. If you're a PHP developer working with Twilio, you might enjoy these other tutorials:

                              Click to Call

                              Put a button on your web page that connects visitors to live support or sales people via telephone.

                              Two-Factor Authentication

                              Improve the security of Laravel's built-in login functionality by adding two-factor authentication via text message.

                              Did this help?

                              Thanks for checking out this tutorial! If you have any feedback to share with us, please reach out on Twitter... we'd love to hear your thoughts, and know what you're building!

                              Mario Celi Agustin Camino David Prothero Andrew Baker  Kevin Whinnery Kat King

                              Need some help?

                              We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.

                              Loading Code Sample...