我有一个使用CRUD操作的laravel应用程序,并且在更新上传的图像时遇到问题。其他所有领域都更新。我不能为我的生活找到问题。
以下是控制器的代码:
public function update(Request $request, $id)
{
// Validate the data
$post = Post::find($id);
$this->validate($request, array(
'title' => 'required|max:255',
'slug' => "required|alpha_dash|min:5|max:255|unique:posts,slug,$id",
'category_id' => 'required|integer',
'body' => 'required',
'featured_image' => 'image'
));
// Save the data to the database
$post = Post::find($id);
$post->title = $request->input('title');
$post->slug = $request->input('slug');
$post->category_id = $request->input('category_id');
$post->body = Purifier::clean($request->input('body'));
if ($request->hasFile('featured_img')) {
//add the new photo
$image = $request->file('featured_img');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images/' . $filename);
Image::make($image)->resize(800, 400)->save($location);
$oldFilename = $post->image;
//update the database
$post->image = $filename;
//delete the old photo
Storage::delete($oldFilename);
}
$post->save();
if (isset($request->tags)) {
$post->tags()->sync($request->tags);
} else {
$post->tags()->sync(array());
}
// set flash data with success message
Session::flash('success', 'This post was successfully saved.');
// redirect with flash data to posts.show
return redirect()->route('posts.show', $post->id);
}
我收到flash消息说该帖子已成功更新,但是,图像仍然相同,甚至没有上传到数据库。我正在使用laravel 5.4。
编辑添加表单代码:
{!! Form::model($post, ['route' => ['posts.update', $post -> id], 'method' => 'PUT', 'files' => true]) !!}
<div class="col-md-8">
<!-- form model binding -->
{{ Form::label('title', 'Title:') }}
{{ Form::text('title', null, ['class' => 'form-control input-lg']) }}
{{ Form::label('slug', 'Slug:', ['class' => 'form-spacing-top']) }}
{{ Form::text('slug', null, ['class' => 'form-control input-lg']) }}
{{ Form::label('category_id', 'Category:')}}
{{ Form::select('category_id', $categories, null, ['class' => 'form-control']) }}
{{ Form::label('tags', 'Tags:', ['class' => 'form-spacing-top']) }}
{{ Form::select('tags[]', $tags, null, ['class' => 'form-control select2-multi', 'multiple' => 'multiple']) }}
{{ Form::label('featured_image', 'Update Featured Image:', ['class' => 'form-spacing-top'] ) }}
{{ Form::file('featured_image')}}
{{ Form::label('body', 'Body:', ['class' => 'form-spacing-top']) }}
{{ form::textarea('body', null, ['class' => 'form-control']) }}
</div>
<div class="col-md-4">
<div class="well">
<div class="dl-horizontal">
<dt>Created At:</dt>
<dd>{{date( 'M j, Y h:ia', strtotime($post->created_at)) }}</dd>
</div>
<div class="dl-horizontal">
<dt>Last Updated:</dt>
<dd>{{date( 'M j, Y h:ia', strtotime($post->updated_at)) }}</dd>
</div>
<hr>
<div class="row">
<div class="col-sm-6">
{!! Html::linkRoute('posts.show', 'Cancel', array($post->id), array('class' => 'btn btn-danger btn-block')) !!}
</div>
<div class="col-sm-6">
{{ form::submit('Save Changes', ['class' => 'btn btn-success btn-block'] ) }}
</div>
</div>
</div>
</div>
{!! Form::close() !!}
答案 0 :(得分:0)