将雄辩的查询转换为float

von4xj4u  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(400)

我有一个雄辩的问题,我想投一个领域作为“浮动”。它是一个聚合值,目前以字符串形式返回,下面是一个查询示例:

$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` 当前作为字符串返回,但我希望它们是浮动的。它们是计算出来的值,所以我不能真正地将属性转换添加到模型中。
编辑:我删除了每个评论的小数点。
c7rzv4ha

c7rzv4ha1#

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

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

相关问题