我是laravel的新手。我正在尝试更改当前登录的用户设置。
这是我的代码。
控制器:
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();
}
刀片:
<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>
似乎陷入了验证之中。我只是在验证后使用了一个返回来检查。
即使没有验证,它仍然无法保存到数据库中。
答案 0 :(得分:1)
Laravel文档说:
update方法需要一组列和值对,表示应该更新的列。
使用$ user-&gt; save()代替