我在 status
表上有几列,例如 upc_status
、is_immediate
、total_value
、price_ticket
、completed_by
、{{1 }}。
客户端可以在工作完成后随时更新状态。我们需要根据 completed_auto_id
让我知道减少代码行的建议或建议来更新 3 个主要状态选项。我所做的如下。
status code
答案 0 :(得分:1)
如果唯一的区别是您正在更新的最后两列,那么您不需要重复代码...此外,if 之后的 ->update()
完全多余...获取模型,更新特定列,然后保存一次。
// Set up variables for use later
$userName = Auth::user()->name;
$userID = Auth::user()->id;
// Fetch the Record from the Database
$objFetch = Order::findOrFail($request->id);
// Set the Columns that will be updated regardless of `status`
$objFetch->upc_status = $request->input('upc_status');
$objFetch->is_immediate = $request->input('is_immediate');
$objFetch->total_value = $request->input('total_value');
$objFetch->price_ticket = $request->input('price_ticket');
// Set specific Columns based on `status`
if ($request->status == 0) {
$objFetch->receiver = $userName;
$objFetch->receiver_auto_id = $userID;
} elseif ($request->status == 2) {
$objFetch->completed_by = $userID;
$objFetch->completed_auto_id = $request->input('price_ticket');
} elseif ($request->status == 3) {
$objFetch->approved_by = $userName;
$objFetch->approved_auto_by = $userID;
}
// Perform a single `save()`
$objFetch->save();
// Return your `json` response
return response()->json([
'success' => true,
'message' => 'Status updated',
], 200);