1

I'm trying to store data from a form in Laravel, but it doesn't work. The page will redirect to itself, without status message and without storing the data.

resources/views/pages/contact.blade.php

        <form action="/contact" class="flex flex-col space-y-8 p-5">
            @csrf
            <div class="flex flex-col space-y-8 md:flex-row md:space-y-0 md:space-x-5">
                <div class="relative">
                    <label for="text">Full Name</label>
                    <input type="text" id="fullName" placeholder="John Doe" />
                </div>
                <div class="relative">
                    <label for="email">E-mail</label>
                    <input type="email" id="email" placeholder="[email protected]" required />
                </div>
            </div>
            <div class="relative">
                <label for="text">Phone Number</label>
                <input type="text" id="phoneNumber" placeholder="(123) 456-7890" />
            </div>
            <div class="relative">
                <label for="textarea">Message</label>
                <textarea type="textarea" id="message" rows="6" placeholder="Message" required></textarea>
            </div>
            <button type="submit">Send</button>
            @if(session('status'))
            <div>
                {{ session('status') }}
            </div>
            @endif
        </form>

routes/web.php

Route::controller(ContactController::class)->group(function () {
    Route::get('/contact', 'create');
    Route::post('/contact', 'store');
});

app/Http/Controllers/ContactController.php

 /**
 * Store a newly created resource in storage.
 *
 * @param  \App\Http\Requests\StoreContactRequest  $request
 * @return \Illuminate\Http\Response
 */
public function store(StoreContactRequest $request)
{
    $contact = new Contact;
    $contact->fullName = $request->fullName;
    $contact->email = $request->email;
    $contact->phoneNumber = $request->phoneNumber;
    $contact->message = $request->message;
    $contact->save();
    
    return redirect('pages.contact')->with('status', 'Message sent!');
}

app/Models/Contact.php

<?php

namespace App\Http\Controllers;

use App\Models\Contact;
use App\Http\Requests\StoreContactRequest;
use App\Http\Requests\UpdateContactRequest;

class ContactController extends Controller
{
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('pages.contact');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \App\Http\Requests\StoreContactRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreContactRequest $request)
    {
        $contact = new Contact;
        $contact->fullName = $request->fullName;
        $contact->email = $request->email;
        $contact->phoneNumber = $request->phoneNumber;
        $contact->message = $request->message;
        $contact->save();
        
        return redirect('pages.contact')->with('status', 'Message sent!');
    }
}

And my migration file

<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('contacts', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->string('full-name');
            $table->string('e-mail');
            $table->string('phone-number');
            $table->string('message');
        });
    }

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

I'm pretty sure I followed every step as best as I could, but somehow it still doesn't work. Thanks for your hindsight!

3
  • It would be great for us to see <input type="email" id="email" required /> instead of <input type="email" class="form-input rounded-lg bg-blue-200 border-none shadow" id="email" placeholder="[email protected]" required /> while reading your question. Commented Mar 11, 2022 at 17:10
  • @medilies You're right, I pruned it a bit. :) Commented Mar 11, 2022 at 17:14
  • Amazing :D I noticed that you shared the controller instead of your model app/Models/Contact.php. But adding method="POST" completely alters the historical accuracy of the answer below Commented Mar 11, 2022 at 17:15

1 Answer 1

1

In your <form> you need method POST.

<form action="/contact" method="POST" class="flex flex-col space-y-8 p-5">

The default method for <form> tag is GET (docs).

You are every time redirected to your GET /contact endpoint.

Sign up to request clarification or add additional context in comments.

4 Comments

It was indeed part of the problem. Now it throws 403 THIS ACTION IS UNAUTHORIZED. How can you be so quick to see such a mistake? Thanks!
@fgodin go to StoreContactRequest and return true from authorize
You are welcome. You can check this answer for your 403 error: stackoverflow.com/questions/47128903/…
@fgodin he did provide you with a valid solution. now your project got one less problem and further issues require their separate questions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.