A Complete Guide (10 Steps) For Creating Contact Form In Laravel 5.7

laravel bgrn image.png.jpg

Laravel

Jan 16, 2019

A Complete Guide (10 Steps) For Creating Contact Form In Laravel 5.7

Laravel is known as the best PHP framework because of its embedded functionality for managing authenticity, caching, sessions and routing very efficiently.
Contact Form adds more functionality which is needed by every website developer for small to large scale website development.
Hence we are going to illustrate all the required steps to create a Contact Us form in laravel 5.7.
Step 1: Install Laravel 5.7
Run below command to Install Laravel 5.7 framework successfully.
composer create-project laravel/laravel contactUs --prefer-dist
After the installation head to the project folder cd contactUs


Step 2: Configure the Mysql Database.
Open . env files and perform following function by replacing

DB_HOST,DB_DATABASE,DB_USERNAME, and DB_PASSWORD.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1 // your host
DB_PORT=3306
DB_DATABASE=homestead // your database name
DB_USERNAME=homestead // your database username
DB_PASSWORD=secret // your database username

 

Step 3: Create the Contact Us table using Database Migration.
Run the following command to create the migration file.
php artisan make:migration create_contact_us_table
Now the migration file is created. Go to database/migration/date_ create_contact_us_table.php and add the following code to it.

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateContactUsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('contactus', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email');
$table->string('subject');
$table->text('message');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop("contactus");
}
}

Now add this command to complete the migration.
php artisan migrate


Step 4: Create the Model.
For creating the model run the following command into the command prompt.
php artisan make:model ContactUs
Now you have successfully created the ContactUs model, go to app/ContactUS.php and add the following code to it.

namespace App;
use Illuminate\Database\Eloquent\Model;
class ContactUS extends Model
{
public $table = 'contactus';
public $fillable = ['name','email','subject','message'];
}
?

Step 5: Create Routes.
Add the following code to routes/web.php file.

Route::get('contact-us', 'ContactUSController@contactUS');
Route::post('contact-us', ['as'=>'contactus.store','uses'=>'ContactUSController@contactSaveData']);

 

Step 6: Create Controller
php artisan make:controller ContactUsController
Run this command and it will create a file named ContactUsController.php in app/Http/Controllers/ directory, open the file add following code to it.

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\ContactUS;
class ContactUSController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function contactUS()
{
return view('contact-us');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function contactSaveData(Request $request)
{
$this->validate($request, [
'name' => 'required',
'email' => 'required|email',
'subject'=>'required',
'message' => 'required'
]);
ContactUS::create($request->all());
return back()->with('success', 'Thanks for contacting us!');
}
}

Step 7: Create Email Template.
For creation of template create a new directory name emails in resources/views/.
Create a file named contactus.blade.php.
Add the following code to it.

<h2>Hello Admin,</h2>
<p>You received an email from : {{ $name }}</p>
<p>Here are the details:</p>
<p>Name: {{ $name }}</p>
<p>Email: {{ $email }}</p>
<p>Subject: {{ $subject }}</p>
<p>Message: {{ $user_message }}</p>
<p>Thank You</p>

Step 8: Configure Email Server.
1. First Login to your Gmail account from which you want to send emails then Click the link "https://myaccount.google.com/?utm_source=OGB&utm_medium=app&pli=1 link."
2. Click on Sign-in & security link and enable two-step verification for Gmail account.
3. Click App Passwords link for generating a password for your mail app.
4. Now, open your .env file and update the following information.

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME= your email address
MAIL_PASSWORD= your Gmail app password
MAIL_ENCRYPTION=tls 

Step 9: Send Email to Admin.
Run below command on terminal to generate your email class.
php artisan make:mail contactus
Now a new email class file name contactus.php under app/Mails directory is generated
Revive Http/Controllers/ContactUSController.php.
Update following code to it.

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\ContactUS;
class ContactUSController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function contactUS()
{
return view('contact-us');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function contactSaveData(Request $request)
{
$this->validate($request, [
'name' => 'required',
'subject' => 'required',
'email' => 'required|email',
'message' => 'required'
]);
ContactUS::create($request->all());
\Mail::send('emails.contactus',
array(
'name' => $request->get('name'),
'email' => $request->get('email'),
'subject' => $request->get('subject'),
'user_message' => $request->get('message')
), function($message) use ($request)
{
$message->from('onlineinterviewquestions@gmail.com');
$message->to('sharadjaiswal1411@gmail.com', 'Admin')->subject($request->get('subject'));
});
return back()->with('success', 'Thanks for contacting us!');
}
}

Happy face!!!
Your contact form is ready for demo.
Now visit yourwebsite.com/contact-us to test and interpret the form.


Conclusion- With the help of this blog you can successfully create a Contact Us page for your laravel website that will save user's information along with sending the information to admin.
We hope by the end of this blog you have successfully created your own Contact Us page. For further information, you may contact Baymediasoft technologies - a web development company, as we are a highly experienced Laravel Development Company. We do provide professional Laravel Development services to our clients so that they get maximum benefit from their project.

If you want to hire Laravel Developers you may contact our business development executives at sales@baymediasoft.com who will assist you accordingly.

Author
user-image-.png
Sheron Kelly

Super Blogger

Be the first to comment!

Leave a comment

recent blogs

Checkout more of our Creative Writings

WordPress The Safest Platform For Entrep...

WordPress Is Self-possessed For Expected Growth As...

J

  • James Smith
  • |
  • 3 months ago
Wordpress Development Services

Creating Your Websites In WordPress Does Not Let Y...

J

  • James Smith
  • |
  • 3 months ago
Blockchain Technology

Blockchain Technology Is Winning Trust With Time....

S

  • Sheron Kelly
  • |
  • 4 months ago
Best WordPress Plugins For Performance O...

WordPress As A Stage Offers A Lot Of Choices For E...

J

  • James Smith
  • |
  • 6 months ago
Quick Steps To Add Header And Footer Cod...

Here We Will Be Discussing Two Methods By Which Yo...

J

  • James Smith
  • |
  • 7 months ago
A Quick Guide For Fixing 500 Internal Se...

This Internal Server Error Screen Surely Panics Ev...

K

  • Kathy Johnson
  • |
  • 7 months ago

clients

head quarter

development center

ping us!

let's get connected