Laravel 5.1我无法更新model $ user

时间:2017-12-11 21:40:33

标签: php laravel-5.1

我有这个功能,我想更新用户背景图像,所以我写道:

public function updateBg(Request $request)
    {
        $user = Auth::user();
        $this->validate($request, [
            'background' => 'image|max:10000',
            // validate also other fields here
        ]);
        // checking file is valid.
        if (!$request->file('background')->isValid()) return redirect()->back()->withErrors(["background" => "File is corrupt"]);

        // file is valid
        $destinationPath = public_path().'/images/Custaccounts/'.$user->id; // upload path
        $extension = $request->file('background')->getClientOriginalExtension(); // getting image extension
        $ran = str_random(5);
        $photo  = $ran.'.'.$extension;

        $request->file('background')->move($destinationPath, $photo); // uploading file to given path

        $bg = $user->id.'/'.$photo;
dd($bg);
        $user->update(['background' => $bg]);

        return Redirect::back();

            }

这条线不会工作:$user->update(['background' => $bg]); dd($ bg)给我正确的字符串和图片上传只是我无法更新我的'背景;现场......

当我写作时:

    $user->update(['background' => $bg, 'name'=>'John']);

名称已更新,但背景不是......在用户模型背景字段中可填写cource

1 个答案:

答案 0 :(得分:1)

如果我理解你的问题,你试图更新用户表格的bg字段..你的代码必须工作。如果不工作试试这个: -

Remove this :- $user->update(['background' => $bg]);
Update this :- User::where('id',Auth::user()->id)->update(['background' => $bg]);

希望它有所帮助!