我有以下查询功能:
$user = User::findOrFail($id)->first();
if($user->status == 1) {
$status = 0;
} else {
$status = 1;
}
User::where("id", $id)->update(["status" => $status]);
当我看到数据库表时,字段status
未更新为1
状态提交说明:
status | int(11) | NO | | 1
答案 0 :(得分:2)
为什么不从实例化的用户对象中保存状态?
你可以这样做:
$user = User::findOrFail($id)->first();
if($user->status == 1) {
$status = 0;
} else {
$status = 1;
}
$user->update(["status" => $status]);
答案 1 :(得分:1)
您可以这样做以切换布尔属性:
$user = User::findOrFail($id);
$user->status = !$user->status;
$user->save();
或者:
$user = User::findOrFail($id);
$user->status = $user->status === 1 ? 0 : 1;
$user->save();