Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jpeterson579's avatar

Creating a contact/feedback form with Laravel?

Can someone please point me in the right direction on creating a simple contact form using laravel? Essential I want to put this in a popup window of some kind to get feedback from people that visit my website.

Are they any tutorial already on here?

0 likes
8 replies
jrean's avatar

Hi, Install Laravel 4.2 with or without Homestead. Set your application (basic stuff). Go to the route file (routes.php). Create a route

Route::get('/contact', function(){
    return View::make('view-file');
});

Route::post('/contact', function(){
   you logic goes here
});

You will find a lot of (free) videos on Laracast. Everything you need to achieve what you want can be found on the documentation: http://laravel.com/docs/4.2

P.s: the popup window will require the use of a third party library (such as Twitter Bootstrap and the modal window) ; but you can also use Javascript or jQuery or a basic page (with maybe a redirect). Popups sucks :p

3 likes
jpeterson579's avatar
jpeterson579
OP
Best Answer
Level 7

Thank you so much for the help. I got it working and now have much better understanding of contact forms and how it relates to post routes and controllers. Posting my solution so hopefully it helps someone else thats just getting started.

My form markup

{{ Form:: open(array('action' => 'ContactController@getContactUsForm')) }} 

<ul class="errors">
@foreach($errors->all('<li>:message</li>') as $message)
{{ $message }}
@endforeach
</ul>

<div class="form-group">
{{ Form:: textarea ('message', '', array('placeholder' => 'Message', 'class' => 'form-control', 'id' => 'message', 'rows' => '4' )) }}
</div>



</div>
<div class="modal-footer">
{{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
{{ Form:: close() }}

Added this to my routed file:

Route::post('contact_request','ContactController@getContactUsForm');

Created Contact Controller With:

public function getContactUsForm(){

        //Get all the data and store it inside Store Variable
        $data = Input::all();

        //Validation rules
        $rules = array (
            //'first_name' => 'required', uncomment if you want to grab this field
            //'email' => 'required|email',  uncomment if you want to grab this field
            'message' => 'required|min:5'
        );

        //Validate data
        $validator = Validator::make ($data, $rules);

        //If everything is correct than run passes.
        if ($validator -> passes()){

           Mail::send('emails.feedback', $data, function($message) use ($data)
            {
                //$message->from($data['email'] , $data['first_name']); uncomment if using first name and email fields 
                $message->from('feedback@gmail.com', 'feedback contact form');
    //email 'To' field: cahnge this to emails that you want to be notified.                    
    $message->to('feedback@gmail.com', 'John')->cc('feedback@gmail.com')->subject('feedback form submit');

            });
            // Redirect to page
   return Redirect::route('home')
    ->with('message', 'Your message has been sent. Thank You!');


            //return View::make('contact');  
         }else{
   //return contact form with errors
            return Redirect::route('home')
             ->with('error', 'Feedback must contain more than 5 characters. Try Again.');

         }
     }
} 
2 likes
ThomasJensen's avatar

@jpeterson579 thx a lot this really helped me, I spent hours searching many pages, this and going back on some of the lessons really worked for me :D

1 like
ATOM-Group's avatar

@jpeterson579, one minor nitpick: your route is a POST request, but your method is called 'getContactUsForm'. I know it sounds silly, but this is the type of thing that you can come back to months later and go "wtf?" over. I recommend renaming the method to "submitContactForm" or something a bit more semantically clear.

"getContactUsForm" would make more sense for the method that handles showing the user the form in the first place (if any)

3 likes
ThomasJensen's avatar

Also a thing which I spent the past hour or more on, never EVER EVER pass variables named "message" to a mail view :D

Because of this: http://laravel.com/docs/4.2/mail

"Note: A $message variable is always passed to e-mail views, and allows the inline embedding of attachments. So, it is best to avoid passing a message variable in your view payload."

lwilliamsjr's avatar

@ThomasJensen ok so what exactly does that mean for the code above? The variable should be changed to something else, or the code needs to be restructured?

lukespoor's avatar

I used 'jpeterson579' code and just get a blank page with a url of /contact_request and no email has come through.

This is the controller code I'm using

<?php

namespace App\Http\Controllers;

class ContactFormController extends Controller
{
    public function getContactUsForm(){
        $data = \Request::all();

        $rules = array (
            'first_name' => 'required',
            'email' => 'required|email',
            'message' => 'required|min:5'
        );

        $validator = \Validator::make ($data, $rules);

        if($validator -> passes()){
            \Mail::send('emails.contact', $data, function($message) use ($data){
                $message->from($data['email'], $data['first_name']);

                $message->to('[email protected]', 'Luke XXXXX')->cc('[email protected]')->subject('Feedback from Contact Form');
            });

            \Route::get('nursery', function () {
                return redirect('nursery');
            });
        } else {
            \Route::get('the-zoo', function () {
                return redirect('the-zoo');
            });
        }
    }
}

Please or to participate in this conversation.