我正在使用 Laravel 开发一个简单的博客项目,我试图将我的创建帖子表单传递的数据存储到我的商店控制器,它不会接受 POST 方法。即使每当我列出我的路线时,我都会看到商店路线接受 post 方法。我使用了一个 get 方法并且有效。在我尝试上传图像之前,它开始告诉我图像文件必须是图像,即使它是图像。然后我发现图像首先没有被表单传递
{{ Form::open(['action' => 'PostsController@store', 'method'=> 'GET', 'enctype' => 'multipart/form-data']) }}
<div class="form-group">
{{Form::label('title', 'Title')}}
{{Form::text('title' , '' , ['class'=> 'form-control', 'placeholder'=> 'this is a title place holder'])}}
</div>
<div class="form-group">
{{Form::label('body', 'body')}}
{{Form::textarea('body' , '' , [ 'id' => 'article-ckeditor' , 'class'=> 'form-control', 'placeholder'=> 'body'])}}
</div>
<div class="form-group">
{{Form::file('cover_image')}}
<input type="hidden" name="_method" value="POST">
</div>
{{Form::submit('Submit',['class'=>"btn btn-info"])}}
{{ Form::close() }}
这是我的控制器
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'body' => 'required',
'cover_image'
]);
//handle file upload
if($request->hasFile('cover_image')){
$image = $request->file('cover_image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('storage/coverimages/' . $filename );
image::make($image)->resize(800, 400)->save($location);
}
else{
echo 'this is shit';
$filename = 'noimage.jpg';
}
//create post
$post = new Post;
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->user_id = auth()->user()->id;
$post->cover_image = $filename;
$post->save();
return redirect('/posts')->with('success', 'Post created');
}