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

《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

就直接写呗…

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

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