我有一个雄辩的查询,我想把一个字段转换成" 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
");
revenue
和change_rate
目前以字符串形式返回,但我希望它们是浮动的。它们是计算值,因此我无法真正将属性转换添加到模型中。
编辑:我从每个评论的查询中删除了DECIMAL。
答案 0 :(得分:0)
将它们添加到模型的$casts
:
protected $casts = [
'revenue' => 'float',
'change_rate' => 'float',
];