{"id":754,"date":"2020-12-28T08:17:37","date_gmt":"2020-12-28T08:17:37","guid":{"rendered":"https:\/\/appfinz.com\/blogs\/?p=754"},"modified":"2024-08-07T06:39:48","modified_gmt":"2024-08-07T06:39:48","slug":"contact-form-in-laravel-7","status":"publish","type":"post","link":"https:\/\/www.appfinz.com\/blogs\/contact-form-in-laravel-7\/","title":{"rendered":"Laravel 9 Contact Form Example Tutorial"},"content":{"rendered":"\n<p>Here I will explain to you how to create a Contact Form in Laravel 7|8 and store the contact form data in MySQL database then you are at the right place.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Create a contact form in laravel 9<\/strong><\/h2>\n\n\n\n<p>Contact Us form is a widely used feature required by almost every web or mobile application. In this Laravel contact form example tutorial, we will learn to create a contact form in Laravel using the Bootstrap 4 CSS Framework, validate contact form using Laravel built-in validation, send email to admin using Mailtrap.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Install Laravel Project<\/h2>\n\n\n\n<p>Install a new Laravel project using given below command.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">composer create-project laravel\/laravel --prefer-dist laravel-contact-form<\/pre><\/div>\n\n\n\n<p>Mode inside the project folder.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">cd laravel-contact-form<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Make Database Connection<\/h2>\n\n\n\n<p>Include your database configuration details in .env file to make the set up the connection between Laravel and MySQL.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">DB_CONNECTION=mysql\nDB_HOST=localhost\nDB_PORT=3306\nDB_DATABASE=laravel_db\nDB_USERNAME=root\nDB_PASSWORD=<\/pre><\/div>\n\n\n\n<p>PL\/SQL<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create Model and Run Database Migrations<\/h2>\n\n\n\n<p>Let us create the Model for contact form, in here we define the table Schema, execute the following command. It will create a Model and migration file. We have to create the table structure by specifying the values in the migrations file and also declare those values in the Model file as a Schema.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">php artisan make:model Contact -m<\/pre><\/div>\n\n\n\n<p>Bash<\/p>\n\n\n\n<p>Open <strong>database\/migrations\/timestamp_create_contacts_table.php<\/strong> file and add the values for contact us form that we stored in the MySQL database.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">&lt;?php\n\nuse Illuminate\\Database\\Migrations\\Migration;\nuse Illuminate\\Database\\Schema\\Blueprint;\nuse Illuminate\\Support\\Facades\\Schema;\n\nclass CreateContactsTable extends Migration\n{\n    \/**\n     * Run the migrations.\n     *\n     * @return void\n     *\/\n    public function up()\n    {\n        Schema::create('contacts', function (Blueprint $table) {\n            $table-&gt;id();\n            $table-&gt;string('name');\n            $table-&gt;string('email');\n            $table-&gt;string('phone');\n            $table-&gt;string('subject');\n            $table-&gt;text('message');\n            $table-&gt;timestamps();\n        });\n    }\n\n    \/**\n     * Reverse the migrations.\n     *\n     * @return void\n     *\/\n    public function down()\n    {\n        Schema::dropIfExists('contacts');\n    }\n}<\/pre><\/div>\n\n\n\n<p>PHP<\/p>\n\n\n\n<p>Add the following code in the <strong>app\/Models\/Contact.php<\/strong> file.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">&amp;lt;?php\n\nnamespace App\\Models;\n\nuse Illuminate\\Database\\Eloquent\\Factories\\HasFactory;\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass Contact extends Model\n{\n    use HasFactory;\n    public $fillable = ['name', 'email', 'phone', 'subject', 'message'];\n}<\/pre><\/div>\n\n\n\n<p>PHP<\/p>\n\n\n\n<p>Next, run the migration using the below command. Once the command is completed, go to the database and check the contacts table created with a name, email, phone, subject, and message values.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">php artisan migrate<\/pre><\/div>\n\n\n\n<p>Bash<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create Contact Form Controller<\/h2>\n\n\n\n<p>Execute the following command to generate the contact form controller file.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">php artisan make:controller ContactUsFormController<\/pre><\/div>\n\n\n\n<p>Bash<\/p>\n\n\n\n<p>Open <strong>app\/Http\/Controller\/ContactUsFormController.php<\/strong> file and add the following code in it.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">&lt;?php\n\nnamespace App\\Http\\Controllers;\nuse Illuminate\\Http\\Request;\nuse App\\Models\\Contact;\n\n\nclass ContactUsFormController extends Controller {\n\n    \/\/ Create Contact Form\n    public function createForm(Request $request) {\n      return view('contact');\n    }\n\n    \/\/ Store Contact Form data\n    public function ContactUsForm(Request $request) {\n\n        \/\/ Form validation\n        $this-&gt;validate($request, [\n            'name' =&gt; 'required',\n            'email' =&gt; 'required|email',\n            'phone' =&gt; 'required|regex:\/^([0-9\\s\\-\\+\\(\\)]*)$\/|min:10',\n            'subject'=&gt;'required',\n            'message' =&gt; 'required'\n         ]);\n\n        \/\/  Store data in database\n        Contact::create($request-&gt;all());\n\n        \/\/ \n        return back()-&gt;with('success', 'We have received your message and would like to thank you for writing to us.');\n    }\n\n}<\/pre><\/div>\n\n\n\n<p>PHP<\/p>\n\n\n\n<p>Import the <strong>Contact<\/strong> Model from the App folder.<\/p>\n\n\n\n<p>Create two functions one for rendering contact form in view and to connect controller to form. The <strong>ContactUsForm()<\/strong> function is useful for storing user data in the database.<\/p>\n\n\n\n<p>We need to define a <code>ContactUsForm()<\/code> function and pass the <code>$request<\/code> parameter to it. To validate the contact us form in Laravel, we will use the request object. This can allow us to use Laravel\u2019s built-in validation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Configure Contact Form Routes in Laravel<\/h2>\n\n\n\n<p>Go to <strong>routes\/web.php<\/strong> file and define the two routes.<\/p>\n\n\n\n<p>The first route is created with the GET method, and it renders the contact form in the view.<\/p>\n\n\n\n<p>The Second route handles the various tasks such as form validation, storing form data in the database and displaying success message to the user on frontend.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">&lt;?php\n\nuse Illuminate\\Support\\Facades\\Route;\nuse App\\Http\\Controllers\\ContactUsFormController;\n\n\/*\n|--------------------------------------------------------------------------\n| Web Routes\n|--------------------------------------------------------------------------\n|\n| Here is where you can register web routes for your application. These\n| routes are loaded by the RouteServiceProvider within a group which\n| contains the &quot;web&quot; middleware group. Now create something great!\n|\n*\/\n\nRoute::get('\/contact', [ContactUsFormController::class, 'createForm']);\n\nRoute::post('\/contact', [ContactUsFormController::class, 'ContactUsForm'])-&gt;name('contact.store');<\/pre><\/div>\n\n\n\n<p>PHP<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Add Custom CSS in Laravel<\/h2>\n\n\n\n<p>In this step, we will learn to add custom CSS in Laravel project. First, create <strong>\/css\/style.css<\/strong> file in <strong>public<\/strong> folder. Also, place the given CSS code in the newly created CSS file.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;CSS&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">.container {\n    max-width: 500px;\n    margin: 50px auto;\n    text-align: left;\n    font-family: sans-serif;\n}\n\nform {\n    border: 1px solid #1A33FF;\n    background: #ecf5fc;\n    padding: 40px 50px 45px;\n}\n\n.form-control:focus {\n    border-color: #000;\n    box-shadow: none;\n}\n\nlabel {\n    font-weight: 600;\n}\n\n.error {\n    color: red;\n    font-weight: 400;\n    display: block;\n    padding: 6px 0;\n    font-size: 14px;\n}\n\n.form-control.error {\n    border-color: red;\n    padding: .375rem .75rem;\n}<\/pre><\/div>\n\n\n\n<p>CSS<\/p>\n\n\n\n<p>Now, you can easily call this CSS file in the Laravel blade template, we need to paste the following code in the <strong>view\/contact.blade.php<\/strong> file to add the custom CSS in Laravel.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">&lt;link rel=&quot;stylesheet&quot; type=&quot;text\/css&quot; href=&quot;{{ asset('css\/style.css') }}&quot;&gt;<\/pre><\/div>\n\n\n\n<p>Markup<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create Laravel 7|8 Contact Form with Bootstrap<\/h2>\n\n\n\n<p>Go to <strong>resources\/views\/<\/strong> folder and create <strong>contact.blade.php<\/strong> file inside of it. Then, add the following code in it.<\/p>\n\n\n\n<p>We created a beautiful contact form using Bootstrap CSS Framework. The <code>@csrf<\/code> tag protects contact form from CROSS Site Request Forgery.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;{{ str_replace('_', '-', app()-&gt;getLocale()) }}&quot;&gt;\n\n&lt;head&gt;\n    &lt;meta charset=&quot;utf-8&quot;&gt;\n    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;\n    &lt;title&gt;Laravel&lt;\/title&gt;\n    &lt;!-- Bootstrap CSS --&gt;\n    &lt;link rel=&quot;stylesheet&quot; href=&quot;https:\/\/stackpath.bootstrapcdn.com\/bootstrap\/4.5.0\/css\/bootstrap.min.css&quot;&gt;\n    &lt;link rel=&quot;stylesheet&quot; type=&quot;text\/css&quot; href=&quot;{{ asset('css\/style.css') }}&quot;&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n    &lt;div class=&quot;container mt-5&quot;&gt;\n\n        &lt;!-- Success message --&gt;\n        @if(Session::has('success'))\n            &lt;div class=&quot;alert alert-success&quot;&gt;\n                {{Session::get('success')}}\n            &lt;\/div&gt;\n        @endif\n\n        &lt;form action=&quot;&quot; method=&quot;post&quot; action=&quot;{{ route('contact.store') }}&quot;&gt;\n\n            &lt;!-- CROSS Site Request Forgery Protection --&gt;\n            @csrf\n\n            &lt;div class=&quot;form-group&quot;&gt;\n                &lt;label&gt;Name&lt;\/label&gt;\n                &lt;input type=&quot;text&quot; class=&quot;form-control&quot; name=&quot;name&quot; id=&quot;name&quot;&gt;\n            &lt;\/div&gt;\n\n            &lt;div class=&quot;form-group&quot;&gt;\n                &lt;label&gt;Email&lt;\/label&gt;\n                &lt;input type=&quot;email&quot; class=&quot;form-control&quot; name=&quot;email&quot; id=&quot;email&quot;&gt;\n            &lt;\/div&gt;\n\n            &lt;div class=&quot;form-group&quot;&gt;\n                &lt;label&gt;Phone&lt;\/label&gt;\n                &lt;input type=&quot;text&quot; class=&quot;form-control&quot; name=&quot;phone&quot; id=&quot;phone&quot;&gt;\n            &lt;\/div&gt;\n\n            &lt;div class=&quot;form-group&quot;&gt;\n                &lt;label&gt;Subject&lt;\/label&gt;\n                &lt;input type=&quot;text&quot; class=&quot;form-control&quot; name=&quot;subject&quot; id=&quot;subject&quot;&gt;\n            &lt;\/div&gt;\n\n            &lt;div class=&quot;form-group&quot;&gt;\n                &lt;label&gt;Message&lt;\/label&gt;\n                &lt;textarea class=&quot;form-control&quot; name=&quot;message&quot; id=&quot;message&quot; rows=&quot;4&quot;&gt;&lt;\/textarea&gt;\n            &lt;\/div&gt;\n\n            &lt;input type=&quot;submit&quot; name=&quot;send&quot; value=&quot;Submit&quot; class=&quot;btn btn-dark btn-block&quot;&gt;\n        &lt;\/form&gt;\n    &lt;\/div&gt;\n&lt;\/body&gt;\n\n&lt;\/html&gt;<\/pre><\/div>\n\n\n\n<p>Markup<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Laravel Contact Form Validation<\/h2>\n\n\n\n<p>We are validating a contact form and accessing the errors for each form input field using <strong>$errors-&gt;has()<\/strong> method.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">&lt;form action=&quot;&quot; method=&quot;post&quot; action=&quot;{{ route('contact.store') }}&quot;&gt;\n\n    @csrf\n\n    &lt;div class=&quot;form-group&quot;&gt;\n        &lt;label&gt;Name&lt;\/label&gt;\n        &lt;input type=&quot;text&quot; class=&quot;form-control {{ $errors-&gt;has('name') ? 'error' : '' }}&quot; name=&quot;name&quot; id=&quot;name&quot;&gt;\n\n        &lt;!-- Error --&gt;\n        @if ($errors-&gt;has('name'))\n        &lt;div class=&quot;error&quot;&gt;\n            {{ $errors-&gt;first('name') }}\n        &lt;\/div&gt;\n        @endif\n    &lt;\/div&gt;\n\n    &lt;div class=&quot;form-group&quot;&gt;\n        &lt;label&gt;Email&lt;\/label&gt;\n        &lt;input type=&quot;email&quot; class=&quot;form-control {{ $errors-&gt;has('email') ? 'error' : '' }}&quot; name=&quot;email&quot; id=&quot;email&quot;&gt;\n\n        @if ($errors-&gt;has('email'))\n        &lt;div class=&quot;error&quot;&gt;\n            {{ $errors-&gt;first('email') }}\n        &lt;\/div&gt;\n        @endif\n    &lt;\/div&gt;\n\n    &lt;div class=&quot;form-group&quot;&gt;\n        &lt;label&gt;Phone&lt;\/label&gt;\n        &lt;input type=&quot;text&quot; class=&quot;form-control {{ $errors-&gt;has('phone') ? 'error' : '' }}&quot; name=&quot;phone&quot; id=&quot;phone&quot;&gt;\n\n        @if ($errors-&gt;has('phone'))\n        &lt;div class=&quot;error&quot;&gt;\n            {{ $errors-&gt;first('phone') }}\n        &lt;\/div&gt;\n        @endif\n    &lt;\/div&gt;\n\n    &lt;div class=&quot;form-group&quot;&gt;\n        &lt;label&gt;Subject&lt;\/label&gt;\n        &lt;input type=&quot;text&quot; class=&quot;form-control {{ $errors-&gt;has('subject') ? 'error' : '' }}&quot; name=&quot;subject&quot;\n            id=&quot;subject&quot;&gt;\n\n        @if ($errors-&gt;has('subject'))\n        &lt;div class=&quot;error&quot;&gt;\n            {{ $errors-&gt;first('subject') }}\n        &lt;\/div&gt;\n        @endif\n    &lt;\/div&gt;\n\n    &lt;div class=&quot;form-group&quot;&gt;\n        &lt;label&gt;Message&lt;\/label&gt;\n        &lt;textarea class=&quot;form-control {{ $errors-&gt;has('message') ? 'error' : '' }}&quot; name=&quot;message&quot; id=&quot;message&quot;\n            rows=&quot;4&quot;&gt;&lt;\/textarea&gt;\n\n        @if ($errors-&gt;has('message'))\n        &lt;div class=&quot;error&quot;&gt;\n            {{ $errors-&gt;first('message') }}\n        &lt;\/div&gt;\n        @endif\n    &lt;\/div&gt;\n\n    &lt;input type=&quot;submit&quot; name=&quot;send&quot; value=&quot;Submit&quot; class=&quot;btn btn-dark btn-block&quot;&gt;\n&lt;\/form&gt;<\/pre><\/div>\n\n\n\n<p>PHP<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Set Up Laravel Email Template<\/h2>\n\n\n\n<p>Create <strong>resources\/views\/mail.blade.php<\/strong> file and add the given below code in it. We will add the contact form values which is sent by site visitor in this template, and you will see these values in your mailbox.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">&lt;h2&gt;Hello&lt;\/h2&gt; &lt;br&gt;&lt;br&gt;\n\nYou have got an email from : {{ $name }} &lt;br&gt;&lt;br&gt;\n\nUser details: &lt;br&gt;&lt;br&gt;\n\nName: {{ $name }} &lt;br&gt;\nEmail: {{ $email }} &lt;br&gt;\nPhone: {{ $phone }} &lt;br&gt;\nSubject: {{ $subject }} &lt;br&gt;\nMessage: {{ $user_query }} &lt;br&gt;&lt;br&gt;\n\nThanks<\/pre><\/div>\n\n\n\n<p>PHP<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Configure Laravel Email Server with Mailtrap<\/h2>\n\n\n\n<p>There are tons of other methods to set up mail a server in Laravel, but we are going to configure Email server in Laravel using Mailtrap.<\/p>\n\n\n\n<p>Go to your <a href=\"https:\/\/mailtrap.io\/\" rel=\"noreferrer noopener\" target=\"_blank\">Mailtrap<\/a> SMTP settings tab, select Laravel from integration drop-down, Laravel provides a clean, simple API over the popular SwiftMailer library. With the default Laravel setup you can configure your mailing configuration by setting these values in the <strong>.env<\/strong> file in the root directory of your project.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">MAIL_MAILER=smtp\nMAIL_HOST=smtp.mailtrap.io\nMAIL_PORT=2525\nMAIL_USERNAME=your_mailtrap_username\nMAIL_PASSWORD=your_mailtrap_password\nMAIL_ENCRYPTION=tls<\/pre><\/div>\n\n\n\n<p>PHP<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Sending Email in Laravel<\/h2>\n\n\n\n<p>Execute the below command to setup the Laravel\u2019s basic email function. Following command will create a new Mailable class with <strong>contact.php<\/strong> file inside the <strong>app\/Mails\/<\/strong> directory.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">php artisan make:mail contact<\/pre><\/div>\n\n\n\n<p>Bash<\/p>\n\n\n\n<p>Again, go to <strong>Http\/Controllers\/ContactUsFormController.php<\/strong> file and replace the entire code with the given below code.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">&lt;?php\n\nnamespace App\\Http\\Controllers;\nuse Illuminate\\Http\\Request;\nuse App\\Models\\Contact;\nuse Mail;\n\nclass ContactUsFormController extends Controller {\n\n    \/\/ Create Contact Form\n    public function createForm(Request $request) {\n      return view('contact');\n    }\n\n    \/\/ Store Contact Form data\n    public function ContactUsForm(Request $request) {\n\n        \/\/ Form validation\n        $this-&gt;validate($request, [\n            'name' =&gt; 'required',\n            'email' =&gt; 'required|email',\n            'phone' =&gt; 'required|regex:\/^([0-9\\s\\-\\+\\(\\)]*)$\/|min:10',\n            'subject'=&gt;'required',\n            'message' =&gt; 'required'\n         ]);\n\n        \/\/  Store data in database\n        Contact::create($request-&gt;all());\n\n        \/\/  Send mail to admin\n        \\Mail::send('mail', array(\n            'name' =&gt; $request-&gt;get('name'),\n            'email' =&gt; $request-&gt;get('email'),\n            'phone' =&gt; $request-&gt;get('phone'),\n            'subject' =&gt; $request-&gt;get('subject'),\n            'user_query' =&gt; $request-&gt;get('message'),\n        ), function($message) use ($request){\n            $message-&gt;from($request-&gt;email);\n            $message-&gt;to('appfinz@gmail.com', 'Admin')-&gt;subject($request-&gt;get('subject'));\n        });\n\n        return back()-&gt;with('success', 'We have received your message and would like to thank you for writing to us.');\n    }\n\n}<\/pre><\/div>\n\n\n\n<p>PHP<\/p>\n\n\n\n<p>Your contact form is up and running. You can store the user data in the MySQL database and send email to the provided email address.<\/p>\n\n\n\n<p>Start the application using the following command.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">php artisan serve<\/pre><\/div>\n\n\n\n<p>Bash<\/p>\n\n\n\n<p>Click on the URL to test the Contact Form application in Laravel:<\/p>\n\n\n\n<p><strong>Conclusion<\/strong>&#8211; With the help of this blog you can successfully create a  <strong>contact form in laravel 7<\/strong><br>We hope by the end of this blog you have successfully created your own Contact Us page. For further information, you may contact<strong> Appfinz Technologies<\/strong> &#8211; a <strong><a href=\"https:\/\/appfinz.com\">web development company<\/a><\/strong>, 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.<\/p>\n\n\n\n<p>If you want to hire Laravel Developers you may contact our business development executives at info@appfinz.com who will assist you accordingly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>contact form in laravel 7 or 8<\/p>\n","protected":false},"author":1,"featured_media":755,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,1],"tags":[115],"class_list":["post-754","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel","category-php","tag-contact-form-in-laravel-7-or-8"],"_links":{"self":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/754","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/comments?post=754"}],"version-history":[{"count":3,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/754\/revisions"}],"predecessor-version":[{"id":882,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/754\/revisions\/882"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/media\/755"}],"wp:attachment":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/media?parent=754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/categories?post=754"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/tags?post=754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}