Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group_concat - laravel eloquent

please I want to use group_concat in a query using eloquent and not raw queries.

here is the code which i tried to execute and did't work for me:

commands::join('products', 'products.id', '=','commands.idproduct')
->select('commands.username','**group_concat(products.name)**')
->group by ('commands. username')
->get();

Thanks in advance :)

like image 535
user3481058 Avatar asked Sep 15 '14 12:09

user3481058


People also ask

What is Group_concat in laravel?

The GROUP_CONCAT() function in MySQL is used to concatenate data from multiple rows into one field. This is an aggregate (GROUP BY) function which returns a String value, if the group contains at least one non-NULL value. Otherwise, it returns NULL.

How to do GROUP CONCAT in Laravel?

Your answerUse 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();


1 Answers

I just used:

use DB;

and in my query I used

DB::raw('group_concat(products.name)')

like image 56
user3481058 Avatar answered Sep 21 '22 16:09

user3481058