码迷,mamicode.com
首页 > 其他好文 > 详细

laravel实践18.分状态处理用户更改信息

时间:2019-12-27 19:41:06      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:赋值   避免   不为   nullable   date   cti   redirect   his   ash   

public function update(User $user, Request $request)
    {
        $this->validate($request, [
            ‘name‘ => ‘required|max:50‘,
            ‘password‘ => ‘nullable|confirmed|min:6‘
        ]);

        $data = [];
        $data[‘name‘] = $request->name;
        if ($request->password) {
            $data[‘password‘] = bcrypt($request->password);
        }
        $user->update($data);

        session()->flash(‘success‘, ‘个人资料更新成功!‘);

        return redirect()->route(‘users.show‘, $user);
    }

户密码验证的 required 规则换成 nullable,这意味着当用户提供空白密码时也会通过验证。如果用户传入password值,则进行confirmed验证。即对传入的 password 进行判断,当其值不为空时才将其赋值给 data,避免将空白密码保存到数据库中。

laravel实践18.分状态处理用户更改信息

标签:赋值   避免   不为   nullable   date   cti   redirect   his   ash   

原文地址:https://www.cnblogs.com/itwatcher/p/12108778.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!