【问题标题】:Submit form data to database提交表单数据到数据库
【发布时间】:2021-12-14 08:58:28
【问题描述】:

我有一个要添加 cmets 的通用文章模型

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

我创建了一个表格,在那里替换了值

<?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::post('send-comment', 'App\Http\Controllers\ArticleController@sendComment')->name('send-comment');

控制器中的功能

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

但是最后,填完表格后,数据不来,评论也没有创建。当我点击提交按钮时,我得到了这个

419 |页已过期

可能是什么问题?如果您需要更多信息,我随时准备提供

【问题讨论】:

    标签: php html laravel


    【解决方案1】:

    您的表单中缺少导致错误的CSRF token

    您应该在表单中添加 CSRF 令牌并将其与发布请求一起发送。

    &lt;form&gt; 标记中添加@csrf 为:

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

    【讨论】:

    • 是的,谢谢,这个还是有问题,但是现在我还是没有文章ID,因为这个错误,可能是什么问题?
    • 我在帖子中添加了我如何尝试在文章页面上获得$article
    • @meow, $artile = Article::find($request-&gt;get('article_id')); 应该是$article = Article::find($request-&gt;get('article_id')); 你的文章字有错别字。
    • 哦该死的,谢谢
    • @meow 如果我回答正确并且对您有所帮助,请接受答案。 :)
    【解决方案2】:

    您的表单上缺少@csrf,如下所示

    <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>
    

    加上你的迁移代码的最后一行,你错过了 $table 并且你只写了 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();
    

    【讨论】:

    • thanks) 在迁移中只是复制中的一个错字
    猜你喜欢
    • 2019-01-19
    • 2013-12-31
    • 1970-01-01
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-03
    相关资源
    最近更新 更多