doubaran2438 2017-03-14 15:58
浏览 95

Laravel更新当前的authed用户设置

I'm new to laravel. I'm trying to change the currently logged in users settings.

Here's my code.

Controller:

use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

public function postUpdateAccount(Request $request)
{
    $this->validate($request, [
        'username' => 'required|max:50|unique:users',
        'email' => 'required|email|unique:users',
        'first_name' => 'required|max:50',
        'last_name' => 'required|max:100',
        'password' => 'required|min:4'
    ]);
    $user = Auth::user();

    $user->username = $request['username'];
    $user->first_name = $request['first_name'];
    $user->last_name = $request['last_name'];
    $user->email = $request['email'];
    $user->password = bcrypt($request['password']);

    $user->update();
}

Blade:

<form action="{{route('account.update')}}" method="post">
{{csrf_field()}}
<label for="username">Username:</label><input class="form-control" type="text" name="username" value="{{$user->username}}" placeholder="Username" autocomplete="off"><br>
<label for="first_name">First Name:</label><input class="form-control" type="text" name="first_name" value="{{$user->first_name}}" placeholder="First Name"><br>
<label for="last_name">Last Name:</label><input class="form-control" type="text" name="last_name" value="{{$user->last_name}}" placeholder="Last Name"><br>
<label for="email">Email Address:</label><input class="form-control" type="email" name="email" value="{{$user->email}}" placeholder="Email Address"><br>
<label for="password">New Password:</label><input class="form-control" type="password" name="password" placeholder="New Password" style="background-image:><br>
<small>You must enter your current password to save any settings. If you do not want to change your password then just add your current password to the "New Password" field.</small>
<br>
<label for="current_password">Current Password:</label><input class="form-control" type="password" name="current_password" placeholder="Current Password"  autocomplete="off"><br>
<button type="submit" class="btn btn-primary">Save Changed</button>

It seems to be getting stuck on the validate. I just used a return after the validate to check that.

Even with the validate not there it still does not save to the database.

  • 写回答

1条回答 默认 最新

  • doujiao7679 2017-03-14 16:09
    关注

    Laravel documentation says :

    The update method expects an array of column and value pairs representing the columns that should be updated.

    Use $user->save() instead

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?