您好我必须编辑和更新带有文件上传的表单。当我编辑其他字段,我也上传了一张照片。但如果我在没有上传照片的情况下编辑其他字段,则显示错误"在非对象"
上调用成员函数getClientOriginalName()我的观点
{{ Form::model($layer, array('method'=>'PATCH', 'route'=>array('edit-layer-patch', $layer->id), 'files'=> true)) }}
<div class="form-group">
<input type="file" name="image" value="{{ $layer->img_path }}" class="form-control" onchange="readURL(this);" id="image" accept="image/*" />
{{ HTML::image('uploads/'. $layer->img_path, 'your image', array('id' => 'blah', 'width'=>150, 'height' => 100)) }}
</div>
<div class="form-group">
<label for="layer_name">Layer Name</label>
<input type="text" class="form-control" name="layer_name" value="{{ $layer->layer_name }}" />
</div>
<div class="form-group">
<label for="select_user">Select User</label>
<select name="select_user" class="form-control">
<option value="{{ $layer->user->id }}">{{ $layer->user->firstname }}</option>
@foreach($users as $user)
<option value="{{ $user->id }}">{{ $user->firstname }}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="select_style">Select Style</label>
<select name="select_style" class="form-control">
<option value="{{ $layer->style->id }}">{{ $layer->style->style_name }}</option>
@foreach($styles as $style)
<option value="{{ $style->id }}">{{ $style->style_name }}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="select_category">Select Category</label>
<select name="select_category" class="form-control">
@if(!empty($layer->category->category_name))
<option value="{{ $layer->category->id }}">{{ $layer->category->category_name }}</option>
@foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->category_name }}</option>
@endforeach
@else
<option value="">Deleted</option>
@foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->category_name }}</option>
@endforeach
@endif
</select>
</div>
<div class="form-group">
<label for="published">Published</label>
<input type="number" class="form-control" min="0" max="1" name="published" value="{{ $layer->published }}" />
</div>
<div class="form-group">
<label for="default">Default</label>
<input type="number" class="form-control" min="0" max="1" name="default" value="{{ $layer->default }}" />
</div>
<div class="form-group">
<button class="btn btn-primary" type="submit">Update</button>
<a class="btn btn-danger" href="{{ route('layers') }}">Cancel</a>
</div>
{{ Form::close() }}
我的控制器
public function edit($id){
$image = Input::file('image');
$filename = $image->getClientOriginalName();
$destinationPath = 'uploads/';
$layer = Layer::find($id);
$validator = Validator::make(Input::all(), Layer::$rules);
if ($validator->fails()) {
return Redirect::route('edit-layer')->withErrors($validator)->with('message', 'Ups something happened');
}else{
//Image::make($image->getRealPath())->save($path);
Input::file('image')->move($destinationPath, $filename);
if(Input::has('image')){
$layer->user_id = Input::get('select_user');
$layer->style_id = Input::get('select_style');
$layer->category_id = Input::get('select_category');
$layer->layer_name = Input::get('layer_name');
$layer->published = Input::get('published');
$layer->default = Input::get('default');
$layer->save();
}else{
$layer->user_id = Input::get('select_user');
$layer->style_id = Input::get('select_style');
$layer->category_id = Input::get('select_category');
$layer->layer_name = Input::get('layer_name');
$layer->published = Input::get('published');
$layer->default = Input::get('default');
$layer->img_path = $filename;
$layer->save();
}
return Redirect::route('layers')->with('message','Layer Updated');
}
}
请帮帮我,谢谢
答案 0 :(得分:2)
因为你这样做:
$image = Input::file('image');
$filename = $image->getClientOriginalName();
即使您没有上传图片(例如$image
是null
)
您需要将所有与图像相关的内容移到if(Input::has('image'))
检查
public function edit($id){
$destinationPath = 'uploads/';
$layer = Layer::find($id);
$validator = Validator::make(Input::all(), Layer::$rules);
if ($validator->fails()) {
return Redirect::route('edit-layer')->withErrors($validator)->with('message', 'Ups something happened');
}else{
if(Input::has('image')){
$image = Input::file('image');
$filename = $image->getClientOriginalName();
Input::file('image')->move($destinationPath, $filename);
// ...
}else{
// ...
}
return Redirect::route('layers')->with('message','Layer Updated');
}
}
来自评论
admin/layers/%7Bid%7D/edit
实际上是admin/layers/{id}/edit
。
看起来验证失败了,它转到Redirect::route('edit-layer')
缺少id参数
这样做:
return Redirect::route('edit-layer', array($id))->withErrors($validator)->with('message', 'Ups something happened');
答案 1 :(得分:0)
您需要将所有图片内容移到if(Input::has('image'))
语句中。