求大佬帮看看原生sql的as,用laravel的模型应该怎么写

$sql = “select count(date) as ct,date from bnb_house_price where housing_id IN($housing_id) AND date >= $begin AND date <= $end and house_stock> 0 GROUP BY date“;

我自己只会这些直来直去的写法,不知道count(date) as ct这里怎么写
BnbHousePrice::query()->whereIn('housing_id', $housing_id)->whereBetween('date',[$begin, $end])->where('house_stock', '>', 0)->groupBy('date')->get('date');

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
最佳答案

就直接写呗…

BnbHousePrice::query()->whereIn('housing_id', $housing_id)->whereBetween('date',[$begin, $end])->where('house_stock', '>', 0)->selectRaw('count(`date`) as ct')->groupBy('date')->get('date');
1年前 评论
MArtian (作者) 1年前
HaoBO (楼主) 1年前
HaoBO (楼主) 1年前
讨论数量: 6

就直接写呗…

BnbHousePrice::query()->whereIn('housing_id', $housing_id)->whereBetween('date',[$begin, $end])->where('house_stock', '>', 0)->selectRaw('count(`date`) as ct')->groupBy('date')->get('date');
1年前 评论
MArtian (作者) 1年前
HaoBO (楼主) 1年前
HaoBO (楼主) 1年前
namespace Illuminate\Database;

class Grammar
{
    /**
     * Wrap a value in keyword identifiers.
     *
     * @param  \Illuminate\Database\Query\Expression|string  $value
     * @param  bool  $prefixAlias
     * @return string
     */
    public function wrap($value, $prefixAlias = false)
    {
        if ($this->isExpression($value)) {
            return $this->getValue($value);
        }

        // 如果要包装的值有一个列别名,那么我们需要分离出各个部分,
        // 以便可以单独包装表达式的每个段,然后使用“as”连接器将它们重新连接在一起。
        if (stripos($value, ' as ') !== false) {
            return $this->wrapAliasedValue($value, $prefixAlias);
        }
        return $this->wrapSegments(explode('.', $value));
    }
}

// 找到了转换的源码

// 可以这样用

DB::table('user')->select(['test as column1','name as user_name'])->limit(10)->get();
1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!