4

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

2 Answers 2

2
+50

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> 
Sign up to request clarification or add additional context in comments.

4 Comments

Yes, thanks, there was still a problem in this, but now I still have no article ID, and because of this error, what could be the problem?
I added to the post how I'm trying to get the $article on the article page
@meow, $artile = Article::find($request->get('article_id')); it should be $article = Article::find($request->get('article_id')); You have a typo in the article word.
now comments are being added, thank you
0

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(); 

1 Comment

thanks) in migration just a typo in copying