gpt4 book ai didi

mysql - Eloquent Query 强制转换为 Float

转载 作者:行者123 更新时间:2023-11-30 21:43:49 25 4
gpt4 key购买 nike

我有一个 Eloquent 查询,我想将一个字段转换为“float”。它是一个聚合值,此时它以字符串形式返回,下面是一个查询示例:

$productStats->selectRaw("
product.id,
TRIM(product.name) AS item_name,
SUM(IF(order.order_paid_date BETWEEN '" . $start->timestamp . "' AND '" . $end->timestamp . "' AND order.order_type = 'order' AND (open_count > 0 OR click_count > 0), order_item.total_price, 0)) as revenue,
COALESCE(CAST(SUM(IF(order.updated_date BETWEEN '" . $start->timestamp . "' AND '" . $end->timestamp . "' AND order.order_type = 'order' AND (open_count > 0 OR click_count > 0), 1, 0)) as SIGNED), 0) / CAST(SUM(IF(order.updated_date BETWEEN '" . $start->timestamp . "' AND '" . $end->timestamp . "' AND order.order_type = 'order', 1, 0)) as SIGNED) AS change_rate
");

revenuechange_rate 目前作为字符串返回,但我希望它们是 float 的。它们是计算值,因此我无法真正向模型添加属性转换。

编辑:我从每条评论的查询中删除了 DECIMAL。

最佳答案

将它们添加到模型的 $casts 中:

protected $casts = [
'revenue' => 'float',
'change_rate' => 'float',
];

关于mysql - Eloquent Query 强制转换为 Float,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50397937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com