无法在不更改当前个人资料照片的情况下更新个人资料详情

时间:2018-02-07 06:25:00

标签: laravel

我第一次成功上传个人资料图片时输入了候选人资料详细信息。当我尝试更新/编辑他们的个人资料时,问题是当我编辑细节时更新个人资料图片它成功更新但当我尝试更改时候选人的详细信息没有改变他们的旧图片数据没有保存。我正在使用laravel。请尝试找到解决此问题的方法..

public function profileupdate(Request $request)
    {
          $est =DB::table('candidate')->where('user_id','=',$request->input('id'))->get();
        $oldimage=$est[0]->image;
        $fileNameToStore=$oldimage;

        if($request->hasFile('image')){
           $this->validate($request,['image'=>'image|nullable|max:1999']);

           //get file name with extension
           $filenameWithExt=$request->file('image')->getClientOriginalName();
           //getjust file name
           $fileName=pathinfo($filenameWithExt, PATHINFO_FILENAME);
           //get jsut ext
           $extension=$request->file('image')->getClientOriginalExtension();
           //file name to store
           $fileNameToStore=$fileName.'_'.time().'.'.$extension;
           // echo $fileNameToStore;die();
           //upload images 
           $path=$request->file('image')->storeAs('public/cand_images',$fileNameToStore);
           $newimage=$fileNameToStore;
       }

       Candidate::where('user_id', Auth::User()->id)
           ->update([
            'phone'=>$request->input('phone'),
            'email'=>$request->input('email'),
            'address'=>$request->input('address'),
            'city'=>$area[0]->area,
             'image'=>$fileNameToStore]);
           File::delete(public_path('storage/cand_images/'.$oldimage));
           return redirect('dashboard')->with('status','Profile Updated Successfully'); 

    }

1 个答案:

答案 0 :(得分:1)

将数据从update方法分离到另一个数组,例如:

$data = ['phone'=>$request->input('phone'),
         'email'=>$request->input('email'),
         'address'=>$request->input('address'),
         'city'=>$area[0]->area];

这样您就可以添加更新图像的附加条件:

if (condition is true) {
   $data['image'] = $fileNameToStore;
}

并将其传递给更新功能:

Candidate::where('user_id', Auth::User()->id)
           ->update($data);