我试图根据没有id的电子邮件更新用户,有没有办法在没有原始查询的情况下执行此操作.
目前的错误
{"error":{"type":"ErrorException","message":"从空值创建默认对象","file":"C:\ wamp\www\celeb-jim\app\controllers\SignupController.php ", "线":189}}
我的守则
Run Code Online (Sandbox Code Playgroud)public function changeAccountStatus ($plan, $userEmail ){ $UpdateDetails = User::where('email', '=', $userEmail)->first(); $UpdateDetails->member_type = $plan; $UpdateDetails->save(); }
mst*_*rdy 15
您可以使用Laravel查询构建器,但这不是最好的方法.
检查Wader在下面的答案是否为Eloquent方式 - 这更好,因为它允许您检查实际上是否有与电子邮件地址匹配的用户,如果没有,则处理错误.
DB::table('users')
->where('email', $userEmail) // find your user by their email
->limit(1) // optional - to ensure only one record is updated.
->update(array('member_type' => $plan)); // update the record in the DB.
Run Code Online (Sandbox Code Playgroud)
如果要更新多个字段,则可以在结尾处向该数组添加更多值.
Wad*_*der 11
此错误表明User::where('email', '=', $userEmail)->first()返回null,而不是更新模型的问题.
在尝试更改用户的属性或使用该firstOrFail()方法之前,请检查您是否确实拥有用户.
$UpdateDetails = User::where('email', $userEmail)->first();
if (is_null($UpdateDetails)) {
return false;
}
Run Code Online (Sandbox Code Playgroud)
或者使用该firstOrFail()方法,不需要检查用户是否为null,因为这会ModelNotFoundException在找不到模型时抛出异常(),您可以使用App::error() http://laravel.com/docs/4.2/errors#handling-错误
$UpdateDetails = User::where('email', $userEmail)->firstOrFail();
Run Code Online (Sandbox Code Playgroud)
尝试这样做。
User::where('email', $userEmail)
->update([
'member_type' => $plan
]);
Run Code Online (Sandbox Code Playgroud)
小智 7
$update = \DB::table('student') ->where('id', $data['id']) ->limit(1) ->update( [ 'name' => $data['name'], 'address' => $data['address'], 'email' => $data['email'], 'contactno' => $data['contactno'] ]);
Run Code Online (Sandbox Code Playgroud)
做起来非常简单。代码如下:
DB::table('user')->where('email', $userEmail)->update(array('member_type' => $plan));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
67574 次 |
| 最近记录: |