![](/img/trans.png)
[英]Call to a member function getClientOriginalName() on null laravel
[英]php - Call to a member function getClientOriginalName() on null and remove alert laravel
我是Laravel的新手,所以我真的需要一些帮助。 我想问一问,当我评论'photo'=>必需的部分时,为什么如果我在不输入照片的情况下进行更新,它会显示一些错误,例如在null上调用成员函数getClientOriginalName()。 因此,真正的问题是我想在不输入照片的情况下进行更新,并且仍应进行更新。
这是我在Controller中上传照片的代码
public function update($id, UpdateBannerRequest $request)
{
$input = $request->all();
//get original file name
$filename = Input::file('photo')->getClientOriginalName();
$input['photo'] = $filename;
Input::file('photo')->move($this->path, $filename);
$banner = $this->BannerRepository->findWithoutFail($id);
if (empty($banner)) {
Flash::error('Banner not found');
return redirect(route('banner.index'));
}
$banner = $this->BannerRepository->update($input, $id);
Flash::success('Banner updated successfully.');
return redirect(route('banner.index'));
}
这是我模型上的代码
<?php
命名空间App \\ Models;
使用Eloquent作为模型; 使用Illuminate \\ Database \\ Eloquent \\ SoftDeletes;
class Banner扩展Model {使用SoftDeletes;
public $table = 'banners';
protected $dates = ['deleted_at'];
public $fillable = [
'title',
'description',
'photo',
'status'
];
protected $casts = [
'title' => 'string',
'description' => 'string',
'photo' => 'string',
'status' => 'integer'
];
public static $rules = [
'title' => 'required',
'description' => 'required',
//'photo' => 'required',
'status' => 'required'
];
}
$validator = Validator::make(
$request->all(),
array(
'photo' => 'required',
),
array(
'photo' => 'Please choose file',
)
);
如果照片不是必须的,请直接使用
if(!empty($request->photo)){
//do something
}
else{
Flash::error('Banner not provided');
return redirect(route('banner.index'));
}
希望这会有所帮助..让我知道是否有任何问题..谢谢
您的更新功能看起来像
public function update($id, UpdateBannerRequest $request)
{
$input = $request->all();
$banner = $this->BannerRepository->findWithoutFail($id);
if(!empty($request->photo)){
//do something for saving the name of file in database and other value respectively using
// $filename = Input::file('photo')->getClientOriginalName();
// $banner->photo = $filename;
}
else{
Flash::error('Banner not provided');
return redirect(route('banner.index'));
}
$banner->save();
Flash::success('Banner updated successfully.');
return redirect(route('banner.index'));
}
所需的最简单验证是测试Input::hasFile('photo')
,是否应在调用Input::file('photo')->getClientOriginalName()
之前放置它
if( Input::hasFile('photo') == false )
{
Flash::error('Banner not provided');
return redirect(route('banner.index'));
}
您应该检查以下代码。
if(isset(Input::file('photo'))
在使用它之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.