user16993770
user16993770

Reputation:

Submit form data to database

I have one general article model that I want to add comments to

Schema::create('article_comments', function (Blueprint $table) {
   $table->BigIncrements('id');
   $table->unsignedBigInteger('article_id');
   $table->foreign('article_id')
      ->references('id')->on('articles')->onDelete('cascade');
   $table->string('name', 128);
   $table->string('email', 128);
   $table->text('text');
   $table->timestamps();

I created a form, substituted values ​​there

<?php

use \App\Models\Article;
    
/**
* @var ArticleBlock $article
*/
?>

<form method="post" action="{{ route('send-comment') }}">
    <div class="row">
        <input type="hidden" name="article_id" value="{{ $article->id }}" />
        <div class="col-md-6">
            <div class="form-item">
                <label for="name">Your Name *</label>
                <div class="input-wrapper">
                    <img src="/img/user.svg" alt="user logo">
                    <input id="name" type="text" name="name">
                </div>
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-item">
                <label for="email">Your Email *</label>
                <div class="input-wrapper">
                    <img src="/img/mail.svg" alt="mail logo">
                    <input id="email" type="email" name="email">
                </div>
            </div>
        </div>
        <div class="col-md-12">
            <div class="form-item">
                <label for="text">Comment *</label>
                <div class="input-wrapper">
                    <img src="/img/comment-alt.svg" alt="comment-alt logo">
                    <textarea id="text" name="text"></textarea>
                </div>
            </div>
        </div>
    </div>
    <input class="submit-button" type="submit" value="Submit Comment">
</form>

Route

Route::post('send-comment', 'App\Http\Controllers\ArticleController@sendComment')->name('send-comment');

Function in the controller

public function sendComment(Request $request)
    {
        $articleComment = new ArticleComment;

        $articleComment->name = $request->get('name');
        $articleComment->email = $request->get('email');
        $articleComment->text = $request->get('text');
        $artile = Article::find($request->get('article_id'));
        $article->article_comments()->save($articleComment);

        return back();
    }

But in the end, after filling out the form, the data does not come and the comment is not created. And when I click on the submit button, I get this

419 | PAGE EXPIRED

What could be the problem? If you need more information, I'm ready to provide

Upvotes: 4

Views: 108

Answers (2)

ndotie
ndotie

Reputation: 2158

Missing @csrf on your form as below

<form method="post" action="{{ route('send-comment') }}">
    @csrf
    <div class="row">
        <input type="hidden" name="article_id" value="{{ $article->id }}" />
        <div class="col-md-6">
            <div class="form-item">
                <label for="name">Your Name *</label>
                <div class="input-wrapper">
                    <img src="/img/user.svg" alt="user logo">
                    <input id="name" type="text" name="name">
                </div>
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-item">
                <label for="email">Your Email *</label>
                <div class="input-wrapper">
                    <img src="/img/mail.svg" alt="mail logo">
                    <input id="email" type="email" name="email">
                </div>
            </div>
        </div>
        <div class="col-md-12">
            <div class="form-item">
                <label for="text">Comment *</label>
                <div class="input-wrapper">
                    <img src="/img/comment-alt.svg" alt="comment-alt logo">
                    <textarea id="text" name="text"></textarea>
                </div>
            </div>
        </div>
    </div>
    <input class="submit-button" type="submit" value="Submit Comment">
</form>

plus final line on your migration codes you miss the $table and you've written just table

Schema::create('article_comments', function (Blueprint $table) {
   $table->BigIncrements('id');
   $table->unsignedBigInteger('article_id');
   $table->foreign('article_id')
      ->references('id')->on('articles')->onDelete('cascade');
   $table->string('name', 128);
   $table->string('email', 128);
   $table->text('text');
   table->timestamps();

Upvotes: 0

OMi Shah
OMi Shah

Reputation: 6186

You're missing the CSRF token in your form which is causing the error.

You should add the CSRF token in your form and send it with the post request.

Add @csrf within the <form> tag as:

<form method="post" action="{{ route('send-comment') }}">
@csrf
...
</form>

Upvotes: 2

Related Questions