求大佬帮看看原生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');

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
最佳答案

就直接写呗…

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年前 评论

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