Home > OS >  Storing data to database with Laravel
Storing data to database with Laravel

Time:03-12

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" >
            @csrf
            <div >
                <div >
                    <label for="text">Full Name</label>
                    <input type="text" id="fullName" placeholder="John Doe" />
                </div>
                <div >
                    <label for="email">E-mail</label>
                    <input type="email" id="email" placeholder="[email protected]" required />
                </div>
            </div>
            <div >
                <label for="text">Phone Number</label>
                <input type="text" id="phoneNumber" placeholder="(123) 456-7890" />
            </div>
            <div >
                <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!

CodePudding user response:

In your <form> you need method POST.

<form action="/contact" method="POST" >

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

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

  • Related