Laravel我的发布方法(商店)表单不起作用

时间:2018-07-06 18:26:08

标签: php laravel forms redirect store

PostsController.php

 public function store(Request $request)
    {
 Post::create($request->all());
 return redirect('/posts');
}

我在帖子目录中查看create.blade.php

@extends('layout.app')



@section('content')

    <h1>Create Post</h1>

    <form action="/posts" method="post">

        <input type="text" name="title" placeholder="Enter title"> <!-- this name=title comes from create_posts_table -->
        {{--{{csrf_field()}}--}}
        <input type="submit" name="submit">

    </form>

@stop

路线

Route::resource('/posts','PostsController');

当我提交浏览器时,它转到localhost / posts并显示:

Object not found!
The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.

If you think this is a server error, please contact the webmaster.

Error 404
localhost
Apache/2.4.33 (Win32) OpenSSL/1.1.0h PHP/7.2.5

没有记录不会保存

2 个答案:

答案 0 :(得分:0)

尝试:

<form action="{{ route('posts.store') }}" method="post">
    //
</form>

,或者您可以阅读this document了解更多信息。并注意您不能通过浏览器调用url发送发帖请求。您可以为此提交表单

答案 1 :(得分:0)

这有两件事。

您正在使用资源路由,因此需要适当地路由到存储方法:

 <form action="{{ route('posts.store') }}" method="post">

此外,您还需要在其他控制器端点上欺骗方法,例如DELETE方法,因为它会因POST请求而失败,您可以这样做:

<form action="{{ route('posts.delete') }}" method="post">
    @csrf
    {{ method_field('DELETE') }}

在这里看看,我只在这段视频https://youtu.be/kPYtJo7RyUQ

中介绍了这一点