Laravel 后文件未更新

File Not Updating in Laravel

我有 Laravel 5.5 申请。我正在尝试更新我的 post,但图像没有更新。当我死掉并转储以下内容时

dd($request->hasFile('image'));

它输出false

查看:

<div class="box-body">
    {{ Form::model($slider, array('route' => array('admin.slider.update', $slider->title), 'method' => 'PUT')) }}
        <div class="form-group">
            {!! Form::label('title', 'Title') !!}
            {!! Form::text('title', null, ['class' => 'form-control', 'placeholder' => 'Title']) !!}
        </div>
        <div class="form-group">
            {!! Form::label('content', 'Content') !!}
            {!! Form::textarea('content', null, ['class' => 'form-control', 'placeholder' => 'Content']) !!}
        </div>
        <div class="row">
            <div class="col-md-2 col-xs-6">
                <div class="form-group">
                    {!! Form::label('image', 'Change Image') !!}
                    {!! Form::file('image') !!}
                </div>
            </div>
            <div class="col-md-10 col-xs-6">
                <img src="{{ secure_asset('images/slider/thumb/' . $slider->image )}}" class="img-responsive" width="150">
                {{ $slider->image }}
            </div>
        </div>
        {!! Form::submit('Submit', array( 'class'=>'btn btn-info' )) !!}
    {!! Form::close() !!}
</div>

控制器:

 public function sliderupdate($slider, Request $request){
    $slider = Slider::where('title', $slider)->firstorfail();
    $slider->title = $request->title;
    $slider->content = $request->content;
    dd($request->hasFile('image'));
    if ($request->hasFile('image')) {
        $file = Input::file('image');
        //getting timestamp
        $timestamp = str_replace([' ', ':'], '-', Carbon::now()->toDateTimeString());
        $name = $timestamp. '-' .$file->getClientOriginalName();
        $file->move(public_path().'/images/slider/', $name);
        $slider->image = $name;
        $thumb = Image::make(public_path().'/images/slider/' . $name)->resize(1920,1080)->save(public_path().'/images/slider/thumb/' . $name, 90);
    }

    $slider->save();
    return redirect()->route('admin.slider.edit', $slider->title)->with('status', 'Update Success');
}

路线:

Route::put('admin/slider/{slider}', [
    'uses'  =>  'AdminController@sliderupdate',
    'as'    =>  'admin.slider.update'
]);

我缺少的东西,但找不到它是什么。

方法:

public function slideredit($slider){ 
    $slider = Slider::where('title', $slider)->firstOrFail(); 
    return view('admin.slider.edit', compact('slider')); 
}

将您的 Form 更改为

{!!  Form::model($slider, ['route' => ['admin.slider.post', $slider->title, 'method' => 'PUT', 'files'=>true]])  !!}
  <div class="modal-body">
    <!-- text input -->
    <div class="form-group">
      {!! Form::label('title', 'Title') !!}
      {!! Form::text('title', null, ['class' => 'form-control', 'placeholder' => 'Title']) !!}
    </div>
    <div class="form-group">
      {!! Form::label('content', 'Content') !!}
      {!! Form::textarea('content', null, ['class' => 'form-control', 'placeholder' => 'Content']) !!}
    </div>
    <div class="form-group">
      {!! Form::label('image', 'Choose Image') !!}
      {!! Form::file('image') !!}
    </div>
  </div>

  <div class="modal-footer">
    <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Close</button>
    {!! Form::submit('Submit', array( 'class'=>'btn btn-info')) !!}
  </div>
{!! Form::close() !!}

您缺少 'files' => true 选项。如果要接受文件,则传递给 Form::openForm::model 的数组中需要 files 选项。

{{ Form::model($slider, [
    'route' => ['admin.slider.update', $slider->title], 
    'method' => 'PUT', 
    'files' => true
]) }}