Today now in this tutorial i will show you how to GROUP_CONCAT with different SEPARATOR in Laravel. Whenever, we need to use GROUP_CONCAT with the different separator(I mean that the default separator is ‘,’, But we want to change it ‘@’,’#’,’&’ etc as we want). So then we need to use use SEPARATOR keyword in GROUP_CONCAT(). Now if we are working on laravel then we must have to also use DB::raw() for write GROUP_CONCAT() inside the this function.
So, now Basically how to change separator in group_concat(), In following example i will change ‘,’ separator into ‘@’.here we can use the following example how to use in laravel application.
$items = DB::table('items') ->select("items.id","items.name" ,DB::raw("(GROUP_CONCAT(items_city.name SEPARATOR '@')) as `cities`")) ->leftjoin("items_city","items_city.item_id","=","items.id") ->groupBy('items.id') ->get();
Read Also : How to use Union query with Laravel Eloquent?
Thanks for read this, I hope it will you. You can also follow us on Facebook.