更新输入字段时出错,Laravel

时间:2018-01-09 10:20:33

标签: php laravel

所以我有这个编辑表格,它包含建筑物名称和图像的字段。

问题是,当我尝试编辑并单击更新建筑物名称时,它显示此错误:

  

在null

上调用成员函数getClientOriginalExtension()

但是当我更改图像并单击更新其工作时,只要您不更改建筑物名称。

以下是代码:

editbuilding.blade.php

 {!! Form::open(array('route' => ['editbuilding',$id], 'class' => 'form' , 'files'=>'true')) !!}
<div class="container">

<div class="form-group">
    {!! Form::label('Building Name') !!}
    {!! Form::text('buildingname', $building->name,        array('required',
              'class'=>'form-control',
              'placeholder'=>'Building Name')) !!}
</div>
<div class="form-group">
  {!! Form::label('Building Name') !!}
  {!! Form::file('buildingpics',
     array('onchange'=>'previewFile()')) !!}


     @if ($building->picture)

   <br/><img src="{{asset('assets/'.$building->picture)}}" id="previewImg" style="height:300px; width:300px;" alt="">
    @else
  </br><p>No image found</p>
@endif

</div>

<div class="form-group">
    {!! Form::submit('Update',
      array('class'=>'btn btn-primary')) !!}
      <a href="{{route('index')}}" class="btn btn-default btn-md">Back</a>
</div>
</div>
{!! Form::close() !!}
<script type="text/javascript">

function previewFile() {
var preview = document.querySelector('#previewImg');
var file    = document.querySelector('input[type=file]').files[0];
var reader  = new FileReader();

reader.addEventListener("load", function () {
  preview.src = reader.result;
}, false);

if (file) {
reader.readAsDataURL(file);
}
}
</script>
@endsection
@section('scripts')
@endsection

BuildingController.php

 public function saveBuilding(Request $request)
    {
          $file = $request->file('buildingpics');
        $building = new Building();
        $building->name = $request->buildingname;
        $building->picture = $building->name.'.'.$file->getClientOriginalExtension();

        $file->move('assets',$building->name.'.'.$file->getClientOriginalExtension());

        $building->save();

       \Session::flash('building_flash', 'Created successfully!');

        return redirect('/');
    }
public function edit($id)
    {
        $building = Building::find($id);
        return view('editbuilding')->withBuilding($building)->with('id',$id);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {

      $building = Building::find($id);
      $building->name = $request->buildingname;

      $file = $request->file('buildingpics');
      $building->picture = $building->name.'.'.$file->getClientOriginalExtension();
      // print_r(FCPATH);

      // dd($file);
      $file->move('assets',$building->name.'.'.$file->getClientOriginalExtension());
      $building->update();

    \Session::flash('building_flash', 'Updated successfully!');

      return redirect()->back();
    }

2 个答案:

答案 0 :(得分:0)

如果有上传的文件,您必须添加测试:

public function update(Request $request, $id)
{

  $building = Building::find($id);
  $building->name = $request->buildingname;

  $file = $request->file('buildingpics');
  if($file != null) {
    $building->picture = $building->name.'.'.$file->getClientOriginalExtension();
    // print_r(FCPATH);

    // dd($file);
    $file->move('assets',$building->name.'.'.$file->getClientOriginalExtension());
  }
  $building->update();

\Session::flash('building_flash', 'Updated successfully!');

  return redirect()->back();
}

答案 1 :(得分:0)

如果请求发布了任何文件,您应该先检查它。您可以使用以下功能进行检查:

$request->hasFile('buildingpics');

所以你的代码应该是这样的:

public function update(Request $request, $id)
{
  ...
  if ($request->hasFile('buildingpics'))
      // do saving file and set picture field here

  }    

  ...
}