【问题标题】:MethodNotAllowedHttpException this wont let me insert the dataMethodNotAllowedHttpException 这不会让我插入数据
【发布时间】:2017-11-26 12:25:01
【问题描述】:

我只是想在数据库上使用 eloquent 插入标题和正文,并且出现 MethodNotAllowedHttpException 不会让我这样做导致该错误

这是控制器

public function store(Request $request)
    {

        $this->validate($request, array(
                'title' => 'required|max:255',
                'body' => 'required'
            ));

        $post = new Post; 

        $post->title = $request->title;
        $post->body = $request->body;

        $post->save();

        return redirect()->route('posts.show', $post->id);

    }

这是我的观点 create.blade.php

<form action="post.store" method="POST">
                <div class="form-group">
                    <label for="title">Title</label>
                    <input type="text" class="form-control" name="title" placeholder="Enter title">
                </div>
                <div class="form-group">
                    <label for="body">Body</label>
                    <textarea class="form-control" name="body" rows="3"></textarea>
                </div>
                <div class="form-group">

                    <input type="submit" class="btn btn-success btn pull-right" value="post">
                </div>
</form>

【问题讨论】:

标签: php laravel laravel-5.4 laravel-eloquent


【解决方案1】:

在表单操作中使用{{ route('posts.store') }}

【讨论】:

    【解决方案2】:

    我假设您正在使用资源路由。

    我认为这个 &lt;form action="post.store" method="POST"&gt; 应该是 &lt;form action="{{ route('post.store') }}" method="POST"&gt; 并且在表单中也使用这个 {{ csrf_field() }} 否则你会得到 tokenmismatch 错误。

    <form action="{{ route('posts.store') }}" method="POST">
    {{ csrf_field() }}
                    <div class="form-group">
                        <label for="title">Title</label>
                        <input type="text" class="form-control" name="title" placeholder="Enter title">
                    </div>
                    <div class="form-group">
                        <label for="body">Body</label>
                        <textarea class="form-control" name="body" rows="3"></textarea>
                    </div>
                    <div class="form-group">
    
                        <input type="submit" class="btn btn-success btn pull-right" value="post">
                    </div>
    </form>
    

    【讨论】:

    • 是的,你的权利我正在使用我尝试过的路线上的资源 {{ route('post.store') }} 并将 {{ csrf_field() }} 放入表单中,我收到一个新错误 ErrorException路线 [post.store] 未定义。
    • 在控制台使用命令php artisan route:list并检查天气post.store是否正确
    • 但是这里它的&lt;form action="{{ route('post.store') }}" method="POST"&gt; 只是更新它&lt;form action="{{ route('posts.store') }}" method="POST"&gt; 它应该可以正常工作。
    • 在您对web.php 进行更改后,最好始终使用php artisan route:cache 来刷新路线。看看你是否仍然面临[post.store] 错误?
    【解决方案3】:

    您可以使用命令php artisan route:list,在名称列中您将看到路线名称,您将可以通过{{ route('routename') }}在视图中调用

    示例: 在路由列表中显示posts.store,因此您将在视图中调用{{ route('posts.store') }}

    我认为您可能会通过上面的路线视图调用不正确的路线posts.show,但插入时您调用post.store 可能是posts.store

    【讨论】:

    • 我总是在我的终端上使用 php artisan route:list 来检查我确定 post.store 的路线名称,我现在正在查看我的路线列表
    • 但我看到你提到的 create.blade.php 是 post.store 而不是 posts.store。应该使用双花括号{{ route('posts.store') }}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    • 2019-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多