updateOrCreate 不會新增的問題
20250717
This commit is contained in:
allen.yan 2025-07-17 16:57:59 +08:00
parent 3adc02ea55
commit 22610d61a5

View File

@ -108,23 +108,33 @@ class RoomControlController extends Controller
$validated['started_at'] = null;
$validated['ended_at'] = null;
}
$room = Room::updateOrCreate(
[
'branch_id' => $validated['branch_id'],
'floor' => $validated['floor'],
'type' => $validated['type'],
'name' => $validated['name'],
],
[
//'internal_ip' => $validated['internal_ip'],
//'port' => $validated['port'],
$room = Room::where([
'branch_id' => $validated['branch_id'],
'floor' => $validated['floor'],
'type' => $validated['type'],
'name' => $validated['name'],
])->first();
if ($room) {
// 更新
$room->update([
'is_online' => $validated['is_online'],
'status' => $validated['status'],
'started_at' => $validated['started_at'],
'ended_at' => $validated['ended_at'],
]
);
]);
} else {
// 新增
$room = Room::create([
'branch_id' => $validated['branch_id'],
'floor' => $validated['floor'],
'type' => $validated['type'],
'name' => $validated['name'],
'is_online' => $validated['is_online'],
'status' => $validated['status'],
'started_at' => $validated['started_at'],
'ended_at' => $validated['ended_at'],
]);
}
return ApiResponse::success(new RoomResource($room->refresh()));
}
}