How to use GROUP_CONCAT in laravel?

0 votes
$assignment = assignment::find(Crypt::decrypt($id));
$assignment_details = $assignment->raw_plan()->groupBy('flag')->get();

I want to following result of this query in laravel

SELECT GROUP_CONCAT(name) AS 'names' FROM `raw_plans` where `assignment_id` = 1 GROUP BY`flag`

Please suggest me how to use GROUP_CONCAT in laravel

Sep 25 in Laravel by kartik
• 31,530 points
42 views

1 answer to this question.

0 votes

Hello @kartik,

You can use relations as query builder to fetch the results as:

$assignment_details = $assignment->raw_plan()
                                ->select(DB::raw('group_concat(name) as names'))
                                ->where('assignment_id', 1)
                                ->groupBy('flag')
                                ->get();

Use table_name.* in select to get all the fields.

$assignment_details = $assignment->raw_plan()
                                ->select('raw_plans.*', DB::raw('group_concat(name) as names'))
                                ->where('assignment_id', 1)
                                ->groupBy('flag')
                                ->get();

Hope it helps!!
Thank you!!

answered Sep 25 by Niroj
• 68,260 points

Related Questions In Laravel

0 votes
1 answer

How to make a constant and use globally in laravel?

Hii, You can create a constants.php page in config folder ...READ MORE

answered Mar 24 in Laravel by Niroj
• 68,260 points
117 views
0 votes
1 answer

How to use multiple databases in Laravel?

Hello @kartik, Laravel has inbuilt support for multiple ...READ MORE

answered Jul 30 in Laravel by Niroj
• 68,260 points
90 views
0 votes
1 answer

How to use API Routes in Laravel?

Hello @kartik, You call it by http://localhost:8080/api/test ...READ MORE

answered Aug 10 in Laravel by Niroj
• 68,260 points
543 views
0 votes
1 answer

How to only use created_at in Laravel?

Hello @kartik, Eloquent does not provide such functionality ...READ MORE

answered Aug 11 in Laravel by Niroj
• 68,260 points
122 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18 in Laravel by Niroj
• 68,260 points
2,893 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18 in Laravel by Niroj
• 68,260 points
351 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23 in Laravel by Niroj
• 68,260 points
240 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23 in Laravel by Niroj
• 68,260 points
3,580 views
0 votes
1 answer

How to use Stored Procedures in Laravel?

Hey, To create a Stored Procedure you can ...READ MORE

answered Mar 24 in Laravel by Niroj
• 68,260 points
1,139 views
0 votes
1 answer

How to use mail() in laravel?

Hello, Laravel provides a powerful and clean API ...READ MORE

answered Mar 24 in Laravel by Niroj
• 68,260 points
98 views