Laravel 5.4 图片更新无效

Laravel 5.4 Image update not working

我有一个带有 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);
}

我收到了闪消息说 post 已成功更新,但是图像仍然是一样的,甚至还没有上传到数据库。我正在使用 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() !!}

@José 回答A.Zapata - 表单中的名称与控制器中的名称不完全匹配。