GROUP_CONCAT with different SEPARATOR in laravel Example
Whenever, you need to use GROUP_CONCAT with differente separator(I mean default separator is ',', But you want to change '@','#','&' etc as you want). then you have to use use SEPARATOR keyword in GROUP_CONCAT(). If you are working on laravel then you have to also use DB::raw() for write GROUP_CONCAT() inside the this function.
So, Basically how to change seprator in group_concat(), In following example i change ',' separator into '@'.you can use the following example how to use in laravel 6, laravel 7, laravel 8 and laravel 9 versions.
class DemoController extends Controller
* Write code on Method
* @return response()
public function index(Request $request)
$items = Item::select(
DB::raw("(GROUP_CONCAT(items_city.name SEPARATOR '@')) as `cities`"))
We are Recommending you
- How to use Carbon in Laravel Blade or Controller File?
- Laravel Eloquent Group By Example
- Laravel Eloquent Delete Record By ID Example
- Laravel Eloquent whereNull() Query Example
- How to use Union query with Laravel Eloquent?
- Laravel Eloquent Relationships Tutorial From Scratch
- Laravel One to One Eloquent Relationship Tutorial
- Laravel One to Many Eloquent Relationship Tutorial
- How to use groupby having with DB::raw in Laravel Query Builder?
- Group by year month example in Laravel using Query Builder
- How to group by multiple columns in Laravel Query Builder?
- Laravel Select with Count Query with Group by Example