In this tutorial, we’ll build a simple contact form with Laravel. You will learn how to work with controllers, models, routing, and Laravel views here. We are going to use to fresh Laravel project we have initialized in the previous post concerning the initiation to Laravel. If you are new to Laravel, we advise you to read that post first to discover what Laravel is and the folder structure of a fresh Laravel project.
This blog post is a part of a series that introduces you to the Laravel world by building a simple contact form protected by Google ReCaptcha and an admin panel to manage messages sent through our contact form.
Presentation of the project
The project we are going to build is a simple contact form with three fields:
- The name
- The email
- The message
When the user will submit the form, we’ll record that information in our database. If you are ready, let’s jump in.
We will use one PHP artisan command to generate our model, controller, and migrations. It’s here where Artisan commands become very handy to work with Laravel.
php artisan make:model Contact -m -c

Generation of Contact model and its associated controller and migrations.
The above command will create a model called Contact, the corresponding controller called ContactController, and finally the corresponding migrations.
The migrations
Our contact form will have three fields that we need to store in our database. Then, we must add those fields in the migrations files reserved for the Contact model.
Navigate to the database->migrations folder and open the file ending by create_contacts_table.php. In that file, add the following line of code as shown in the below picture.
$table->string('name');
$table->string('email');
$table->text('message');

migrations file for the model Contact
The complete code for this file is the following.
create_contacts._table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class 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->text('message');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('contacts');
}
};
Database connection
The next step is the configuration of the connexion to a database. Login to your database through a command line interface or via the phpMyAdmin web interface and create a database called our_first_app. After creating that database, come to the .env file and configure the database connexion like this:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=our_first_app
DB_USERNAME=root
DB_PASSWORD=mystrongPassword
Note: The DB_USERNAME is the username (in my case root) you use to connect to your DBMS(Database Management System) MySQL/MariaDB, and DB_PASSWORD is the password you use for that connexion (mystrongPassword in my case).

database configuration
After configuring the access to a database by our application, we can apply migrations with the command:
php artisan migrate

php artisan migrate output

Table contacts in our database
The following step is to edit our Contact model file to add fillable attributes. The fillable attributes are the fields of the table that can be filled from an array to directly create a new record in our database.
Navigate to app -> Models and open the Contact.php file to add the fillable attribute like this:
protected $fillable = [
'name',
'email',
'message'
];
The complete code for this file is the following:
Contact.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Contact extends Model
{
use HasFactory;
protected $fillable = [
'name',
'email',
'message'
];
}
The contact controller has two functions:
- The index function which is responsible to show the contact form locate at resources/views/contact.blade.php
- The store function is used to store the information sent by the users in the database.
Let’s explain a little bit the different parts of the store function.
$this->validate($request, [
'name' => 'required',
'email' => 'required',
'message' => 'required'
]);
This piece of code is used to validate the user request. It checks in the request sent by the user whether there are name, email, and message attributes there; otherwise, the request is aborted.
$requestData = $request->all();
Once the request has all the attributes, the previous code is used to retrieve all the data sent by a user from the Request object. Those data will be saved in the variable $requestData in a form of an array. For example:
$requestData = [
'name' => 'John',
'email' => 'john@example.com',
'message' => 'A test message'
]
The following code will use the $requestData array to create a Contact object and store it in the database. This process is made easy thanks to the fillable attribute we have set in the Contact models
Contact::create($requestData);
The last line redirects users to the contact view with a success message. The complete code for this file is the following:
ContactController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Contact;
class ContactController extends Controller
{
public function index()
{
return view('contact');
}
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
'email' => 'required',
'message' => 'required'
]);
$requestData = $request->all();
Contact::create($requestData);
return redirect('contact')->with("status", "Your message has been sent");
}
}
The routing
Now, we’ll add two routes in the routes/web.php file: One to show the contact form and the other one to submit the data entered in the contact form by the users. The complete code of this file is the following.
web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ContactController;
Route::get("/contact", [ContactController::class, 'index']);
Route::post("/send-message", [ContactController::class, 'store']);
Here, we have first imported the ContactController class. After that, we have created routes according to what HTTP method we want them to be accessible. The first argument of those methods is the route that will be used in the web browser, the second argument is the controller where to route the request and the method responsible for handling the request.
The views
The last part of this tutorial concern the view file. So, go to the folder resources/views and create the file contact.blade.php and paste in the following code.
contact.blade.php
<html>
<head>
<title>Contact Form</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
<form method="post" action="/send-message">
@csrf
@if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif
<h3 class="text-center">Contact form</h3>
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" id="email" class="form-control" name="email">
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" id="message" rows="5" name="message"></textarea>
</div>
<button type="submit" class="btn btn-primary">Send message</button>
</form>
</div>
</body>
</html>
In the form tag, we specify that we want to send our data using the POST method to the route “/send-message”.
The @csrf is responsible to generate a CSRF(Cross-Site Request Forgery) token to protect our application from CSRF attacks via our contact form.
The code below is responsible to display a success message once we successfully recorded the request of the user.
@if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif
Start your server with the command php artisan serve and go to http://127.0.0.1:8000/contact to test the contact form.

First test

Second test
Now, Check the result in your database if the message was successfully recorded.

The message we have sent through the contact form
Conclusion
In this blog post, we have explained to you how to build a simple contact form using the Laravel Framework. We have explored the core concepts of Laravel that are used to build complex applications. In the next tutorial, we’ll build a simple admin panel to view the message sent by users via our contact form. You can find the complete code for this tutorial here. Hope we have enjoyed it. If you have any questions, write to us, and we’ll quickly reply to you.