尝试创建帖子时,PostsController @ store不起作用

时间:2020-03-06 01:13:46

标签: laravel

这是我的部分,我想将表单发送到PostsController中的store函数,但是当我点击Submit时,它显示419页已过期,但是它应该返回到posts页面,并且应该将帖子存储在数据库中。

@section('content')
    <form action="{{ action('PostsController@store') }}" method="POST">
        <div class="form-group">
            <label for="title">Title</label>
            <input type="text" id="title" name="title" placeholder="Title">
        </div>
        <div class="form-group">
            <label for="body">Body</label>
            <textarea id="body" name="body" placeholder="Body"></textarea>
        </div>
        <button type="submit" class="btn btn-default">Save</button>
    </form>
@endsection

这是我在PostsController中的存储功能

public function store(Request $request)
    {
        //create post
        $post = new Post;
        $post->title = $request->input('title');
        $post->body = $request->input('body');
        $post->save();

        return redirect('/posts');
    }

1 个答案:

答案 0 :(得分:0)

419页已过期错误通常是由csrf令牌问题引起的。

尝试在表单中添加@csrf

<form action="{{ action('PostsController@store') }}" method="POST">
   @csrf
   // etc
</form>