我正在尝试在编辑表单中加载用户当前图像(图像URL保存在db中)... 如果未选择编辑表单中的新图像,则会出现此错误...
未定义的变量:image_path
这是我的更新功能
public function update(Request $request,User $user)
{
$get_user = Auth::user();
if(Input::hasFile('image'))
{
$user_email = $get_user->email;
$image = request()->file('image');
$image_extension = $image->guessClientExtension();
$image_path = $image->storePubliclyAs('public/uploads/profile_images/images', $user_email . ".{$image_extension}");
}
$user->firstname = $request->get('firstname');
$user->lastname = $request->get('lastname');
$user->company= $request->get('company');
$user->address= $request->get('address');
$user->phone = $request->get('phone');
$user->country= $request->get('country');
$user->zipcode = $request->get('zipcode');
$user->city = $request->get('city');
$user->state = $request->get('state');
$user->skills = $request->get('skills');
$user->interests = $request->get('interests');
$user->about = $request->get('about');
$user->website = $request->get('website');
$user->image_url = $image_path;
$user->save();
$request->session()->flash('message.level', 'success');
$request->session()->flash('message.content', 'Profile Updated Successfully !');
return redirect()->route('dashboard');
}
以下是编辑表单中的图像字段..
{{ Form::label('image_url', 'Profile Image') }}
{{ Form::file('image' ,null, ['class' => 'form-control','id'=>'image_url']) }}
如果用户未在“编辑”表单中选择图像,请帮助并为我提供处理场景的解决方案..
答案 0 :(得分:1)
如果阻止$user->image_url = $image_path;
,您可以移动if(Input::hasFile('image'))
。错误将返回,因为在编辑中您没有选择图像,因此如果代码中的阻塞不起作用,那么$ image_path将是未定义的
public function update(Request $request,User $user)
{
$get_user = Auth::user();
if(Input::hasFile('image'))
{
$user_email = $get_user->email;
$image = request()->file('image');
$image_extension = $image->guessClientExtension();
$image_path = $image->storePubliclyAs('public/uploads/profile_images/images', $user_email . ".{$image_extension}");
$user->image_url = $image_path;
}
$user->firstname = $request->get('firstname');
$user->lastname = $request->get('lastname');
$user->company= $request->get('company');
$user->address= $request->get('address');
$user->phone = $request->get('phone');
$user->country= $request->get('country');
$user->zipcode = $request->get('zipcode');
$user->city = $request->get('city');
$user->state = $request->get('state');
$user->skills = $request->get('skills');
$user->interests = $request->get('interests');
$user->about = $request->get('about');
$user->website = $request->get('website');
$user->save();
$request->session()->flash('message.level', 'success');
$request->session()->flash('message.content', 'Profile Updated Successfully !');
return redirect()->route('dashboard');
}