$assignment = assignment::find(Crypt::decrypt($id));
$assignment_details = $assignment->raw_plan()->groupBy('flag')->get();
我想在laravel
中关注此查询的结果SELECT GROUP_CONCAT(name) AS 'names' FROM `raw_plans` where `assignment_id` = 1 GROUP BY`flag`
请建议我如何在laravel中使用GROUP_CONCAT
答案 0 :(得分:18)
您可以使用关系作为查询构建器来获取结果:
$assignment_details = $assignment->raw_plan()
->select(DB::raw('group_concat(name) as names'))
->where('assignment_id', 1)
->groupBy('flag')
->get();
<强>更新强>
在select中使用table_name.*
获取所有字段。
$assignment_details = $assignment->raw_plan()
->select('raw_plans.*', DB::raw('group_concat(name) as names'))
->where('assignment_id', 1)
->groupBy('flag')
->get();
答案 1 :(得分:7)
shoieb有些正确,但您应该在访问DB:raw()
你应该试试这个:
$data = DB::table('raw_plans')
->select(DB::raw("group_concat(raw_plans.name)"))
->groupBy('flag')
->where('assignement_id',1)
->get();
希望这有帮助。
答案 2 :(得分:4)
尝试使用以下代码
$data = DB::table('raw_plans')
->select(DB::raw("GROUP_CONCAT(name SEPARATOR '-') as `names`"))
->groupBy('flag')
->where('assignement_id',1)
->get();
答案 3 :(得分:1)
试试这个
$sql = "SELECT GROUP_CONCAT(name) AS 'names' FROM `raw_plans` where `assignment_id` = 1 GROUP BY`flag`";
$info = DB::select(DB::raw($sql));