In this tutorial you will learn about the Create Validate Laravel 8 Contact Form with Send Email and its application with practical example.
In this Create Validate Laravel 8 Contact Form with Send Email tutorial I will show you how to Create a contact form with validation in laravel. We will also send an email to admin on form submission and save contact form input in the database.
In this tutorial, you will learn to create a contact form UI using Bootstrap CSS in laravel. I will also show you how to validate contact form, sending email to the admin with contact form values using mailtrap.
Create & Validate Laravel 8 Contact Form with Send Email
In this step by step tutorial I will demonstrate you how to create a contact form in laravel with validation and sending email on form submission.
- Install Laravel Project
- Setup Database
- Create Migration
- Add Route
- Add Controller
- Add Blade.
Step 1: Install Laravel Project
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2: Setup Database
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.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=here your database name DB_USERNAME=here database username DB_PASSWORD=here database password |
Step 3: Create Migration
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan make:migration Contacts |
After this command you will find one file in following path “database/migrations” and you have to put bellow code in your migration file for create students table
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateContactsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('contacts', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email'); $table->string('phone'); $table->string('subject'); $table->text('message'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('contacts'); } } |
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan migrate |
Now, we will put following code in Contact model :
App/Models/Contact.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Contact extends Model { use HasFactory; /** * The attributes that are mass assignable. * * @var array */ public $fillable = [ 'name', 'email', 'phone', 'subject', 'message' ]; } |
Step 4: Add Route
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
1 2 3 4 |
use App\Http\Controllers\ContactController; Route::get('/contact-form', [App\Http\Controllers\ContactController::class, 'contactForm'])->name('contact-form'); Route::post('/contact-form', [App\Http\Controllers\ContactController::class, 'storeContactForm'])->name('contact-form.store'); |
Step 5: Add Controller
Now, lets create a controller named ContactController using command given below –
1 |
php artisan make:controller ContactController |
Now, we will add following two methods in ContactController
1)contactForm()
2)storeContactForm()
app/Http/Controllers/ContactController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Contact; use Mail; class ContactController extends Controller { public function contactForm() { return view('contactForm'); } public function storeContactForm(Request $request) { $request->validate([ 'name' => 'required', 'email' => 'required|email', 'phone' => 'required|digits:10|numeric', 'subject' => 'required', 'message' => 'required', ]); $input = $request->all(); Contact::create($input); // Send mail to admin \Mail::send('contactMail', array( 'name' => $input['name'], 'email' => $input['email'], 'phone' => $input['phone'], 'subject' => $input['subject'], 'message' => $input['message'], ), function($message) use ($request){ $message->from($request->email); $message->to('admin@admin.com', 'Admin')->subject($request->get('subject')); }); return redirect()->back()->with(['success' => 'Contact Form Submit Successfully']); } } |
Step 6: Add Blade
Now, we will create two blade file contactForm and second is mail file.
1) contactForm.blade.php
2) contactMail.blade.php
So let’s just create following file and put bellow code.
resources/views/contactForm.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
<!DOCTYPE html> <html> <head> <title>Laravel 8 Contact Form Example</title> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha512-MoRNloxbStBcD8z3M/2BmnT+rg4IsMxPkXaGh2zD6LGNNFE80W3onsAhRcMAMrSoyWL9xD7Ert0men7vR8LUZg==" crossorigin="anonymous" /> </head> <body> <div class="container"> <div class="row mt-5 mb-5"> <div class="col-8 offset-2 mt-5"> <div class="card"> <div class="card-header bg-info"> <h3 class="text-white">Laravel 8 Contact Form Example</h3> </div> <div class="card-body"> @if(Session::has('success')) <div class="alert alert-success"> {{ Session::get('success') }} @php Session::forget('success'); @endphp </div> @endif <form method="POST" action="{{ route('contact-form.store') }}"> {{ csrf_field() }} <div class="row"> <div class="col-md-6"> <div class="form-group"> <strong>Name:</strong> <input type="text" name="name" class="form-control" placeholder="Name" value="{{ old('name') }}"> @if ($errors->has('name')) <span class="text-danger">{{ $errors->first('name') }}</span> @endif </div> </div> <div class="col-md-6"> <div class="form-group"> <strong>Email:</strong> <input type="text" name="email" class="form-control" placeholder="Email" value="{{ old('email') }}"> @if ($errors->has('email')) <span class="text-danger">{{ $errors->first('email') }}</span> @endif </div> </div> </div> <div class="row"> <div class="col-md-6"> <div class="form-group"> <strong>Phone:</strong> <input type="text" name="phone" class="form-control" placeholder="Phone" value="{{ old('phone') }}"> @if ($errors->has('phone')) <span class="text-danger">{{ $errors->first('phone') }}</span> @endif </div> </div> <div class="col-md-6"> <div class="form-group"> <strong>Subject:</strong> <input type="text" name="subject" class="form-control" placeholder="Subject" value="{{ old('subject') }}"> @if ($errors->has('subject')) <span class="text-danger">{{ $errors->first('subject') }}</span> @endif </div> </div> </div> <div class="row"> <div class="col-md-12"> <div class="form-group"> <strong>Message:</strong> <textarea name="message" rows="3" class="form-control">{{ old('message') }}</textarea> @if ($errors->has('message')) <span class="text-danger">{{ $errors->first('message') }}</span> @endif </div> </div> </div> <div class="form-group text-center"> <button class="btn btn-success btn-submit">Save</button> </div> </form> </div> </div> </div> </div> </div> </body> </html> |
resources/views/contactMail.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<h2>Hey !</h2> <br><br> You received an email from : {{ $name }} <br><br> User details: <br><br> Name: {{ $name }}<br> Email: {{ $email }}<br> Phone: {{ $phone }}<br> Subject: {{ $subject }}<br> Message: {!! $subject !!}<br><br> Thanks |
Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
localhost:8000/contact-form |