//Earlier in the code, in each Model:
query = ModelName::select('table_name.*')
//Later in the code in a function in a Trait class that is always called
if ($column == 'group_by')
{
$thing_query->groupBy($value);
$thing_query->select(DB::raw('COUNT('.$value.') as count'));
}
有没有办法在 Eloquent 查询构建器中附加或包含单独的选择函数?
实际的 ->select() 设置较早,然后调用此函数。我想在后面的函数中有条件地添加计数列,该函数已将查询传递给它。
最佳答案
为了将来引用,您可以使用 addSelect() 函数。
在文档中有它会很好,但您会在 API 中找到它:http://laravel.com/api/4.2/Illuminate/Database/Query/Builder.html#method_addSelect
关于mysql - Laravel Fluent 在不同的地方添加 select()s?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17284914/