Laravel Contact Form API

Laravel 8 Contact Form API

Posted by

In this tutorial, we are going to create a Laravel 8 Contact Form API and will store the form data in MySQL Database.

Contact Us form is a commonly used feature required by almost every web or mobile application. In this Laravel contact form tutorial, we will learn to create a contact form API in Laravel 8, validate contact form API using Laravel built-in validation, store data in the database and send email to application admin using Gmail SMTP.

You can check the article Laravel 8 Register and Login Rest API Using Passport

The following are the steps to create Laravel 8 Contact Form API:

  1. Laravel 8 Installation
  2. Database connection
  3. SMTP Email Configuration
  4. Model and Contact table Creation Using Migrations
  5. Controller Creation And Laravel Contact Form Validation
  6. Setup Email Template
  7. API Route Creation
  8. Send Email

1. Laravel 8 Installation

To new set up of Laravel8, run the below command:

2. Database Connection

Got to your installed Laravel project and open the .env file and setup database:

3. SMTP Email Configuration

First, you need to change the Gmail security setting. Logged in to Gmail and Go to Manage Your Google Account-> Security and change like below:

Google Account Setting

Now open the .env file and update like below:

4. Model and Contact table Creation Using Migrations

Run the below command to create a model and Migration file.

Open database/migrations/your_timestamp_create_contacts_table.php file and add the values for the contact form that you have to store in the MySQL database.

Open the app/Models/Contact.php file and add the following code.

First, using the below instruction, run the migration. If the command is ended, go to the database and check the name, email, subject, and message fields of the contacts table.

5. Controller Creation And Laravel Contact Form Validation

Run the below command to create a controller:

And add the below code:

6. Setup Email Template

Create an email template resources/emails/contactemail.blade.php and add the below code:

7. API Route Creation

Open routes/api.php and add the below code

8. Send Email

Now, the contact form API setup has been completed. You can validate the form data, store the data in the database and send the contact details to the application admin.

Run the below command to run the application:

Open the postman and run the post API(http://127.0.0.1:8000/api/contact) and pass the name, email, subject, and message in the body like below:

Laravel8 Contact Form API

 

 

Conclusion:

In this tutorial, we have learned Laravel 8 Contact Form Rest API. We have set up Laravel, Created Contact Form API, Validate the form post data, and stored it in the Database. We have tested the Contact Form Rest API using Postman.

You can download the code from Github.

Leave a Reply

Your email address will not be published. Required fields are marked *