Laravel 5.1 我无法更新模型 $user

Laravel 5.1 I cant update model $user

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

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']);

名称已更新,但背景未更新...在用户模型背景字段中,当然可以填写

如果我理解你的问题,你正在尝试更新用户 table 的 bg 字段。你的代码必须有效。如果不起作用试试这个:-

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

希望对您有所帮助!