php - 在 null 时调用成员函数 getClientOriginalName() 并删除警报 laravel
php - Call to a member function getClientOriginalName() on null and remove alert laravel
我是 Laravel 的新手,所以我真的需要一些帮助。我想问问什么时候
我评论了部分 'photo' => required 为什么如果我在不输入照片的情况下进行更新,它会显示一些错误,例如在 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 横幅扩展模型
{
使用软删除;
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'
];
}
所需的最简单验证是测试是否 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'))
使用它之前。
$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'));
}
我是 Laravel 的新手,所以我真的需要一些帮助。我想问问什么时候 我评论了部分 'photo' => required 为什么如果我在不输入照片的情况下进行更新,它会显示一些错误,例如在 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 横幅扩展模型 { 使用软删除;
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'
];
}
所需的最简单验证是测试是否 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'))
使用它之前。
$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'));
}