PT-83
3 months ago
592
18
Laravel

Contact form not passing data to database?

Posted 3 months ago by PT-83

Hi all,

I have a contact form that I'm testing out. However, once the data is entered the form refreshes and nothing is passed to MySQL database. I have tried tinkering with the route, view, controller but still can figure this one out.

Hoping someone can point me in the right direction on how to address this issue?

Web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Mail\WelcomeMail;

Route::group(['middleware' => ['web']], function () {
    //
});

Route::group(['middleware' => 'web'], function () {
    Route::auth();

    Route::get('/', function () {
        return view('welcome');
    });

Route::get('/home', '[email protected]');
});

Route::get('/about', function () {
    return view('about');
}); 

Route::get('/email', function () {
    return new WelcomeMail();
});

Auth::routes();

Route::get('/posts', '[email protected]');
Route::get('/posts/create', '[email protected]');
Route::post('/posts/create', '[email protected]');
Route::get('/posts/{post}', '[email protected]');
Route::get('/posts/{post}/edit', '[email protected]');
Route::patch('/posts/{post}', '[email protected]');
Route::delete('/posts/{post}', '[email protected]');

Route::post('/register', 'Auth\[email protected]')->name('register');

Route::get('/contact', '[email protected]');
Route::post('/contact', '[email protected]');

ContactController


<?php

namespace App\Http\Controllers;

use App\Contact;
use App\Mail\ContactMail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class ContactController extends Controller
{    
    public function create()
    {
        return view('contact.create');
    }
    
    public function store()
    {
        $data = request()->validate([
            'name' => 'required|min:5', 
            'email' => 'required|email',
            'text' => 'required'
        ]);

        \App\Contact::create($data);

        return redirect()->back();

        Mail::to('[email protected]')->send(new ContactMail($data));
    }
}
    

Form

@extends('layouts.app')

@section('content')

<div class="container">
    <div class="row justify-content-center">
       <div class="col-md-8">
           <div class="card">
               <div class="card-header">Contact Famijam</div>

                <div class="card-body">
                    <form action="/contact" method="POST">
                        
                       @csrf

                        <div class="form-group">
                            <label for="name">Name</label>
                            <input name="name" type="name" class="form-control" id="name" aria-describedby="nameHelp" placeholder="Enter Your Name" autocomplete="off" value="">

                        </div>

                        <div class="form-group">
                            <label for="email">Email</label>
                            <input name="email" type="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="Enter Your Email" autocomplete="off" value="">

                        </div>

                          <div class="form-group">
                            <label for="text">Text</label>
                            <input name="text" type="text" class="form-control" id="text" aria-describedby="textHelp" placeholder="Message" autocomplete="off" value="">

                        </div>

                        <button type="submit" class="btn btn-primary">Submit</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

Lastly, Contact_Table Migration


<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateContactsTable 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->longText('text');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('contacts');
    }
}

Contact Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
    protected $guarded = [];
}

Please sign in or create an account to participate in this conversation.