【发布时间】:2014-11-08 23:04:32
【问题描述】:
请在使用 eloquent 而不是原始查询的查询中使用 group_concat。
这是我尝试执行但对我不起作用的代码:
commands::join('products', 'products.id', '=','commands.idproduct')
->select('commands.username','**group_concat(products.name)**')
->group by ('commands. username')
->get();
提前致谢:)
【问题讨论】:
-
尝试使用 DB::raw() 在查询中包装你想要的“原始”SQL:
->select('commands.username',DB::raw('group_concat(products.name)')) -
感谢您的回复@alexrussell,但问题是当我添加 DB::raw 并再次运行应用程序时,我收到此错误:Symfony \ Component \ Debug \ Exception \ FatalErrorException Class 'Storage \Commands\DB' 未找到
-
啊,您在命名空间中 - 在命名空间声明后添加
use DB;或在代码中使用DB:\DB::raw('group_concat(products.name)') -
非常感谢@alexrussell,现在我添加了使用数据库;而且效果很好:)
标签: laravel eloquent group-concat