Support the ongoing development of Laravel.io →
Database Eloquent

Hi guys! I have a query in laravel that does the job in 99%.

$users = \DB::table('company_user')
                ->leftJoin('users', 'users.id', '=', 'company_user.user_id')
                ->where('company_user.company_id', '=', $company_id)
                ->where('company_user.type', '=', $type)
                ->whereNull('company_user.deleted_at')
                ->leftJoin(\DB::raw("(SELECT cud.company_user_id, GROUP_CONCAT(cd.hierarchy_name SEPARATOR '<br>') as departments_list, GROUP_CONCAT(cd.id SEPARATOR ',') as departments_ids FROM company_user_departments as cud JOIN company_departments as cd ON cd.id = cud.company_department_id GROUP BY cud.company_user_id) as cud"), function($join){
                    $join->on("cud.company_user_id", "=" , "company_user.id");
                })
                ->leftJoin(\DB::raw("(SELECT cul.company_user_id, GROUP_CONCAT(cp.value SEPARATOR '<br>') as locations_list, GROUP_CONCAT(cp.id SEPARATOR ',') as locations_ids FROM company_user_locations as cul JOIN company_parameters as cp ON cp.id = cul.location_id GROUP BY cul.company_user_id) as cul"), function($join){
                    $join->on("cul.company_user_id", "=" , "company_user.id");
                })
                ->select($select)
                ->groupBy('company_user.id');

I cant manage to add extra conditional freetext into locations_list (<br> separated list). Basically, if in company_user_location column manages_unallocated == 1, then i want to add a record (once) into locations_list like ......<br>Unallocated

Last updated 3 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

ensimek ensimek Joined 26 Apr 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.