[英]Laravel Not Accessing The Request of data post
Here I create a form in Laravel to post request through route "posts.store" to the controller "PostsController" 在这里,我在Laravel中创建了一个表单,以通过路由“ posts.store”将请求发布到控制器“ PostsController”
{!! Form::open(['url' => 'posts.store']) !!}
{{Form::Label('title','Title:')}}
{{Form::Text('title',null,array('class' => 'form-control'))}}
{{Form::Label('body','Post Body:')}}
{{Form::Textarea('body',null,array('class' => 'form-control'))}}
{{Form::Submit('Create Post',array('class' => 'btn btn-success btn-lg btn-block','style' => 'margin-top:20px;'))}}
{!! Form::close() !!}
This is the "PostsController" where I am trying to access my request which throughs an error: 这是我尝试通过错误访问我的请求的“ PostsController”:
public function store(Request $request)
{
//validate data
$this-> validate($request, array(
'title' => 'required|max:255',
'body' => 'required'
));
//Store data into database
$post = new Post;
$post->title = $request->title;
$post->body = $request->body;
$post->save();
return redirect()->route('posts.show', $post->id);
}
When I submit the form the following page is appearing 当我提交表单时,将显示以下页面
NotFoundHttpException: NotFoundHttpException:
Change this: 更改此:
{!! Form::open(['url' => 'posts.store']) !!}
to this: 对此:
{!! Form::open(['url' => 'PostController@store']) !!}
To get PostData you need this: 要获取PostData,您需要这样做:
$postData = $this->request->all();
$post->title = $postData['title'];
$post->body = $postData['body'];
In your NotFoundException screenshot you can see the URL it is going to is http://127.0.0.1:8000/posts.store obviously this is not going to work. 在您的NotFoundException屏幕截图中,您可以看到它要访问的URL是http://127.0.0.1:8000/posts.store显然这是行不通的。 Change the {!! Form::open(['url' => 'posts.store']) !!}
更改{!! Form::open(['url' => 'posts.store']) !!}
{!! Form::open(['url' => 'posts.store']) !!}
to Form::open(['action' => 'PostsController@store', 'method' => 'POST'])
. {!! Form::open(['url' => 'posts.store']) !!}
到Form::open(['action' => 'PostsController@store', 'method' => 'POST'])
。 Also, make sure your POST route is setup in web.php. 另外,请确保在web.php中设置了POST路由。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.