我正在尝试根据不存在 id 的电子邮件更新用户,有没有一种方法可以在没有原始查询的情况下执行此操作。
当前错误
{"error":{"type":"ErrorException","message":"Creating default object from empty value","file":"C:\wamp\www\celeb-jim\app\controllers\SignupController.php","line":189}}
我的代码
public function changeAccountStatus ($plan, $userEmail ){ $UpdateDetails = User::where('email', '=', $userEmail)->first(); $UpdateDetails->member_type = $plan; $UpdateDetails->save(); }
最佳答案
您可以使用 Laravel query builder ,但是这不是最好的方法。
检查 Wader's answer below对于 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.
如果您有多个字段要更新,您只需在末尾向该数组添加更多值即可。
关于php - Laravel 更新查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27248753/