validate([ 'status' => [ 'required', 'integer', // ensure the status ID exists in your statuses table Rule::exists('stock_entries_status', 'id'), ], 'count' => ['nullable', 'integer', 'min:0'], ]); // Create the history record $history = StockEntryStatusHistory::create([ 'stock_entries_id' => $stockEntry->id, 'stock_entries_status_id' => $data['status'], 'status_note' => isset($data['count']) ? "Count: {$data['count']}" : null, 'section_id' => null ]); return response()->json([ 'message' => 'Status updated successfully.', 'history' => $history, ], 201); } }