如何在laravel中更新数据库之前更改字段的值

时间:2017-06-18 11:31:03

标签: php mysql laravel

首先让我们查看我的代码。这是我的控制器部分:

 public function store(Request $request){

        $data=$request->all();
        $adminProfile=AdminProfile::find(1);

        $image = $request->file('admin_propic')->getClientOriginalName();
        $gallery = new AdminProfile();
        $gallery->admin_propic= $image;
        $adminProfile->admin_propic=$image;
        $request->admin_propic->move(public_path('uploaded'),$image);

        $request->admin_propic = $image;
        if($adminProfile){
            $adminProfile->update($request->all());
        }
        else{
            AdminProfile::create($data);
        }

         return view('dashboard part-admin.edit_profile');
    }

现在,问题是,当我更新个人资料图片(admin_propic)时,它设置临时图片的路径而不是它的原始名称。但是,在我的"上传"文件夹,正在上传个人资料图片的真实姓名。所以,我试图从控制器部分改变它的价值。但它没有用。

** $ image包含所需的值

1 个答案:

答案 0 :(得分:1)

试试这个:

public function store(Request $request){

        $data=$request->all();
        $adminProfile=AdminProfile::find(1);

        $image = $request->file('admin_propic')->getClientOriginalName();
        $gallery = new AdminProfile();
        $gallery->admin_propic= $image;
        $adminProfile->admin_propic=$image; 
        $request->admin_propic->move(public_path('uploaded'),$image);

       // $request->admin_propic = $image;  this will not work i think as request object is readonly as far as i know.
        if($adminProfile){ 
            // updated here 
            $data['admin_propic'] = $image
            $adminProfile->update($data);
        }
        else{
            AdminProfile::create($data);
        }

         return view('dashboard part-admin.edit_profile');
    }