Laravel – Custom contact form and send email

Laravel custom contact form and send email are another routine functionality that is a requirement of more or less every website. In this article, I am going to demonstrate how you can easily create a contact form and mail function in Laravel with email sending.

Laravel is well known PHP Framework, providing multiple solutions for a problem. This is one of the main reasons of the popularity of the PHP framework. Most popular solutions cover this framework routine functionality such as authentication, sessions, routing, send email and caching.

Creating custom contact form in Laravel

  • Configure Laravel Project
  • Install HTML and Form Package
  • Configure Project Database
  • Create Database Table Using Migrations
  • Create ContactUs Model
  • Create Page Route contact
  • Create ContactUs Controller
  • Create contact Page View
  • Sending Email Function

Configure Laravel Project

Install and configure your Laravel project by using the Composer create-project command in your terminal: composer create-project laravel/laravel laraveldemo –prefer-dist “laraveldemo” is your project directory.

Install HTML and Form Package

To create custom contact form in Laravel, I will use laravelcollective/html package for contact us Forms and HTML. Launch the SSH terminal and login to your server using the Master Credentials and go to your project root directory. Run command composer require laravelcollective/html

After successful installed HTML and Forms package laravelcollective/html, goto config/app.php file and add Autoloaded Service Provider and Class Alias.

Configure Project Database

Now configure your project database connection, Find .env file in your project root directory, Open .env and add database access credentials there.

Create Database Table Using Migration Command

Go to your SSH terminal and Run artisan make:migration create_YOUR_TABLE_NAME_table command to create the database migration for creating the Contact Us model.

Contact us migration has been created, go to database/migration/2020_04_02_180504_create_contact_us_table.php and update up method public function up() with database fields like below.

Now run the artisan migrate command to generated database table contact_us with all (id, name, email, message, created_at, updated_at) fields.

Create ContactUs Model

Go to your SSH terminal and Run make:model MODEL_NAME command to create ContactUS model.

After successfully created the ContactUS model, go to App directory and Find file app/ContactUS.php and add the following code in the file.

Create Page Route contact

Now, We will go to create the route “contact-us” (Contact Us page URL). Go to routes/web.php and add the following code to the route:

Create ContactUs Controller

After setup page the routes, I will go to create a controller to handle the requests from the routes. Go to your SSH terminal and Run make:controller CONTROLLER_NAME command to create the controller.

Go to app/Http/Controllers directory and find and Open ContactUSController.php and add the following code.

Create contact Page View

Now I am going to create contact us Page View layout of the contact form. Go to resources/views/ and create a file contactus.blade.php. Place the following code in this blade (view) file.

Form output like below picture.

Sending Email Function

Custom contact us form is now functional. On submitting the form, the data will be stored in the database. I will now make the form email send function.

Now create another blade (view) file, resources/views/email.blade.php and add the following code in email blade.

Now go to project directory, Find .env file, Open .env and add SMTP email access credentials there.

Now SMTP email the configuration is done, update app/Http/Controllers/ContactUsController.php with the following code.

Laravel custom contact us form is now ready for deployment. This module save the data in the database and send email it to the given email address as well. To test the Contact Us form, go to the application and click on launch application (remember to add /contact-us to the URL). Thanks.

Thank you for carefully read my article If you have any query please Leave your query on following comment section or contact with us. Please LIKE and SHARE


Laravel – PHP Artisan console commands with Laravel Framework 7.3

Introduction to Artisan

Artisan is the console command line interface included with Laravel Framework. Artisan provides a number of helpful commands that can assist you while you build your Laravel application.

How to use commands?

To view list of available Artisan commands by following command.


Each command also includes a “help” screen which displays and describes the command’s available arguments and options. We can view a help screen the name of the command with help

Available Aartisan console commands with description.

Below some most common uses commands with details:


Every time when we run different commands on the Laravel application laravel framework generates automatic some cache files in bootstrap/cache folder. Those files are compiled.php and services.php. Both of them file keeps list of mostly used classes/services needed by your project configuration. This php artisan clear-compiled command removes all the files from cache folder.


To enable maintenance mode, we need to execute the down Artisan command: php artisan down Put the application into maintenance mode.


To disable maintenance mode, we need to execute the up Artisan command: php artisan up Bring the application out of maintenance mode.


Create Local Development Server by artisan serve command php artisan serve The serve command is used to run the application using the PHP development server. This command is generally used for development and testing purposes.


To generating migrations for database each table migration file name contains a timestamp which allows Laravel to determine the order of the migrations php artisan make:migration create_users_table migration file created on “database\migrations” directory. We can use the –path option when executing the make:migration command, If we would like to specify a custom output path for the generated migration.


Laravel artisan db:seed command is used to add records to a database automatically using a Seeder Illuminate\Database\Seeder class to generate or provide the records. For the db:seed defines three options: class, database and force. By default the db:seed command will use for the DatabaseSeeder class, this class is defines in the project root database/seeds/DatabaseSeeder.php file. Importance seeder commands are below.


The user auth:clear-resets command can be used to remove expired password reset tokens from the database.


Flush the application cache.
Remove an item from the cache.
Create a migration for the cache database table.
Create a cache file for faster configuration loading.
Remove the configuration cache file.
Discover and cache the application’s events and listeners.
Clear all cached events and listeners.
Generate the missing events and listeners based on regist.

Thank you for carefully read my article If you have any query please Leave your query on following comment section or contact with me. Please LIKE and SHARE


Laravel install in localhost XAMPP server

Laravel install in localhost XAMPP server, please execute following 7 steps.

1. Download XAMPP for Windows, Linux or OSX Installer from here:
Download XAMPP and make ready your local web server.

2. Go to localhost web root directory E:\xampp\htdocs
Create a new folder for your project like “laraveldemo”

3. Run Command Prompt for Windows
Go to project root directory E:\xampp\htdocs

4. Download and run the Composer Windows Installer from here:
Download Composer

Download Composer
Download Composer

5. Check the installation using composer -V
Output: Composer version 1.10.1 2020-03-13 20:34:27

6. If Composer is works fine, than go to install laravel using the following command.
composer create-project laravel/laravel laraveldemo
composer create-project laravel/laravel laraveldemo –prefer-dist “laraveldemo” is your project directory.

laravel composer create-project

7. After complete install and create laravel project we need to create Local Development Server by artisan command. Go to project root directory E:\xampp\htdocs\laraveldemo and run artisan command php artisan serve

php artisan serve command

Browser your laravel project using url on browser and enjoy!

Laravel demo project

Thank you for carefully read my article If you have any query please Leave your query on following comment section or contact with me. Please LIKE and SHARE