How to Create Contact Form In Laravel 8 Example Tutorial

In this tutorial you will learn about the How to Create Contact Form In Laravel 8 Example Tutorial and its application with practical example.

In this How to Create Contact Form In Laravel 8 Example Tutorial I’ll show you how to create simple contact form with validation in laravel 8. In this tutorial you will learn to create contact form with validation and send mail in laravel 8. In this article I will also show you how to submit and validate contact form data before submit in laravel. We will also send an email when contact form is submitted then save contact form input in the database. In this example I will guide you through creating contact form in laravel, validating contact form, submitting contact form and then sending email when contact form is submitted.

How to Create Contact Form In Laravel 8 Example Tutorial

In this step by step tutorial I will demonstrate you how to create simple contact form with validation and send email in laravel. Please follow the instruction given below:

  1. Install Laravel Project
  2. Make Database Connection
  3. Model and Migrations
  4. Create Controller
  5. Configure Contact Form Routes
  6. Add Custom CSS in Laravel
  7. Create Laravel Contact Form with Bootstrap
  8. Laravel Contact Form Validation
  9. Set Up Email Template
  10. Set Up Laravel Email Server
  11. Send Email in Laravel

Install Laravel Project

First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command

switch into the project directory:

Make Database Connection

Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.

Create Model and Run Database Migrations

Now, in this step we will create model and migration file. Please run the following command:

Open database/migrations/timestamp_create_contacts_table.php file and add the values for contact us form that we stored in the MySQL database.

Next, open app/Models/Contact.php file and add the $fillable property in the model as following.

Now, run the migration to create database table using following artisan command:

Create Contact Form Controller

Now, lets create a controller named ContactUsFormController using command given below –

Open app/Http/Controller/ContactUsFormController.php file and add the following code in it.

Configure Contact Form Routes in Laravel

After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.

routes/web.php

Add Custom CSS in Laravel

Lets create /css/style.css file in public folder and put the below CSS code in the newly created CSS file.

Now, you can easily call this CSS file in the Laravel blade template, we need to paste the following code in the view/contact.blade.php file to add the custom CSS in Laravel.

Create Laravel 8 Contact Form with Bootstrap

Go to resources/views/ folder and create contact.blade.php blade view file inside of it. Then, put the following code in it.

Laravel Contact Form Validation

Now we will add validation on contact form input field using $errors->has() method.

Set Up Laravel Email Template

Create resources/views/mail.blade.php file and add the given below code in it.

Configure Laravel Email Server with Mailtrap

Sending Email in Laravel

Now use the following command to create a new Mailable class with contact.php file inside the app/Mails/ directory.

Now, go to Http/Controllers/ContactUsFormController.php file and put the following code with the given below code.

Now we are ready to run our example so lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

In this tutorial we have learn about the How to Create Contact Form In Laravel 8 Example Tutorial and its application with practical example. I hope you will like this tutorial.