0
私は、CRUD操作のラーベルアプリケーションを持っていて、アップロードした画像の更新に問題があります。他のフィールドはすべて正常に更新されます。私の人生は問題を見つけることができません。ここでLaravel 5.4画像の更新がうまくいかない
は、コントローラのコードです:
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);
}
私はポストが正常に更新されたことを言うためにフラッシュメッセージを受信しています、しかし、画像はまだ同じであるとさえにアップロードされていませんデータベース。私はlaravel 5.4を使用しています。
編集フォームのコードを追加する:@JoséA.Zapataによって回答
{!! 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() !!}
画像がアップロードされていない可能性があります。フォームコードを投稿してください。 –
@JoséA.Zapataそこに行く – spbrad
コントローラでは 'if($ request-> hasFile( 'featured_img'))'と書かれていますが、フォームのファイル名は 'featured_image'です。 –