在 laravel 中向数据库添加任务然后重定向

Add a task to database in laravel then redirect

我正在 laravel 制作一个简单的任务应用程序,以巩固我的头脑。我有一个带有表单的页面设置,用于创建任务然后将其提交到数据库,然后重定向回主页。当我点击提交时,它给我这个屏幕:

该页面因不活动而已过期。 请刷新后重试

我在 GitHub 上有这个项目:https://github.com/fullstackfox16/laravel-task

任何帮助将不胜感激!

你的问题能否具体一点。通常,当使用 HTML 表单时,您可以在使用消息包处理您的请求后使用重定向返回向您的用户显示消息反馈,如果您有白屏或其他异常情况,请确保您没有错误语法

我发现您忘记在表单中添加 csrf_token 字段。

<form method="POST" action="/posts">
  <input type="hidden" name="_token" value="{{ csrf_token() }}"> <!-- THIS LINE --> 
  <div class="field">
    <label class="label">Task</label>
    <div class="control">
      <textarea class="textarea" placeholder="Textarea" name="body"></textarea>
    </div>
  </div>

  <div class="field is-grouped">
    <div class="control">
      <button class="button is-primary">Add Task</button>
    </div>
  </div>

</form>

如果问题仍然存在,请清理浏览器缓存并重试。

有两种方法可以解决这个问题:

其一,在您的表单中添加 csrf_field()。又好又简单。

或者

您可以从 VerifyCsrfToken 中间件中省略该路由。

app/Http/Middleware/VerifyCsrfToken.php

 protected $except = [
        'your/route'
    ];