You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel请求验证后整数字段转为字符串的问题咨询

解决Laravel请求验证后整数字段返回字符串的问题

HTTP请求中的所有参数默认都是字符串类型,Laravel的integer验证规则仅检查值是否可解析为整数,不会自动转换类型,因此validated()返回的仍是字符串格式的数值。以下是几种解决方式:

方法一:重写请求类的validated方法

在你的请求类中重写validated方法,对指定字段进行类型转换:

public function validated()
{
    $data = parent::validated();

    // 定义需要转为整数的字段(包含嵌套字段)
    $intFields = [
        'total_capital_cost',
        'investment_amount_id',
        'other_income.other_income_sources',
        'other_income.income_frequency',
        'overseas_transfers_last_year.funds_transfer_country',
        'incoming_funds_last_year.funds_origin_country',
    ];

    foreach ($intFields as $field) {
        if (str_contains($field, '.')) {
            // 处理嵌套字段
            [$parentKey, $childKey] = explode('.', $field, 2);
            if (isset($data[$parentKey][$childKey])) {
                $data[$parentKey][$childKey] = (int)$data[$parentKey][$childKey];
            }
        } else {
            // 处理顶级字段
            if (isset($data[$field])) {
                $data[$field] = (int)$data[$field];
            }
        }
    }

    return $data;
}

方法二:在验证前转换类型(prepareForValidation)

利用请求类的prepareForValidation方法,在验证阶段之前就将字段转为整数,确保验证和后续使用的都是正确类型:

protected function prepareForValidation()
{
    // 转换顶级字段
    $this->merge([
        'total_capital_cost' => $this->input('total_capital_cost') ? (int)$this->input('total_capital_cost') : null,
        'investment_amount_id' => $this->input('investment_amount_id') ? (int)$this->input('investment_amount_id') : null,
    ]);

    // 转换other_income嵌套字段
    if ($this->has('other_income')) {
        $otherIncome = $this->input('other_income');
        $otherIncome['other_income_sources'] = isset($otherIncome['other_income_sources']) ? (int)$otherIncome['other_income_sources'] : null;
        $otherIncome['income_frequency'] = isset($otherIncome['income_frequency']) ? (int)$otherIncome['income_frequency'] : null;
        $this->merge(['other_income' => $otherIncome]);
    }

    // 转换overseas_transfers_last_year嵌套字段
    if ($this->has('overseas_transfers_last_year')) {
        $transfers = $this->input('overseas_transfers_last_year');
        $transfers['funds_transfer_country'] = isset($transfers['funds_transfer_country']) ? (int)$transfers['funds_transfer_country'] : null;
        $this->merge(['overseas_transfers_last_year' => $transfers]);
    }

    // 转换incoming_funds_last_year嵌套字段
    if ($this->has('incoming_funds_last_year')) {
        $funds = $this->input('incoming_funds_last_year');
        $funds['funds_origin_country'] = isset($funds['funds_origin_country']) ? (int)$funds['funds_origin_country'] : null;
        $this->merge(['incoming_funds_last_year' => $funds]);
    }
}

方法三:控制器内手动转换

如果不想修改请求类,也可以在控制器调用validated()后直接处理:

$validated = $request->validated();

// 转换顶级字段
$validated['total_capital_cost'] = (int)$validated['total_capital_cost'];
$validated['investment_amount_id'] = (int)$validated['investment_amount_id'];

// 转换嵌套字段
if (isset($validated['other_income'])) {
    $validated['other_income']['other_income_sources'] = (int)$validated['other_income']['other_income_sources'];
    $validated['other_income']['income_frequency'] = (int)$validated['other_income']['income_frequency'];
}
// 其他嵌套字段同理处理...

转换后的数据存入JSON字段或返回给前端时,就会保持整数类型,避免前端处理时的数据类型不一致问题。

内容的提问来源于stack exchange,提问作者Vüsal Hüseynli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 11:17:08