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, 2020 in Laravel by kartik
• 37,510 points
6,793 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, 2020 by Niroj
• 82,880 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, 2020 in Laravel by Niroj
• 82,880 points
3,581 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, 2020 in Laravel by Niroj
• 82,880 points
2,094 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, 2020 in Laravel by Niroj
• 82,880 points
7,283 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, 2020 in Laravel by Niroj
• 82,880 points
2,646 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, 2020 in Laravel by Niroj
• 82,880 points
21,750 views
0 votes
1 answer

What is redirection in Laravel?

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

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
2,647 views
0 votes
1 answer

How to install Laravel via composer?

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

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
2,504 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, 2020 in Laravel by Niroj
• 82,880 points
41,371 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, 2020 in Laravel by Niroj
• 82,880 points
4,527 views
0 votes
1 answer

How to use mail() in laravel?

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

answered Mar 24, 2020 in Laravel by Niroj
• 82,880 points
611 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP