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@example.com" 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!

fgodin
  • 77
  • 8
  • It would be great for us to see `` instead of `` while reading your question. – medilies Mar 11 '22 at 17:10
  • @medilies You're right, I pruned it a bit. :) – fgodin Mar 11 '22 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 – medilies Mar 11 '22 at 17:15

1 Answers1

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.

mare96
  • 3,749
  • 1
  • 16
  • 28
  • 1
    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 Mar 11 '22 at 17:09
  • 2
    @fgodin go to `StoreContactRequest` and `return true` from `authorize` – medilies Mar 11 '22 at 17:12
  • 1
    You are welcome. You can check this answer for your 403 error: https://stackoverflow.com/questions/47128903/errors-this-action-is-unauthorized-using-form-request-validations-in-laravel – mare96 Mar 11 '22 at 17:15
  • @fgodin he did provide you with a valid solution. now your project got one less problem and further issues require their separate questions – medilies Mar 11 '22 at 17:29