0
CRUD 작업이 있고 업로드 된 이미지를 업데이트하는 데 문제가있는 laravel 응용 프로그램이 있습니다. 다른 모든 필드는 잘 업데이트됩니다. 나는 내 인생이 그 문제를 발견 할 수는 없다.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'입니다. –