Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

noblemfd's avatar

Query not giving expected result in Laravel

In my Laravel-5.8, I have this Query:

From the query above, I want to display all the employees, either the record matches or not. Then, compare the matching records.

The problem now is that whenever there is no matching record for work_location_id for any employee as in:

->join('hr_work_locations AS l', function($join) use ($userCompany)
   {
     $join->on('l.id', '=', 'e.work_location_id')
     ->where('l.company_id', '=', $userCompany);
   }) 

it won't add the employee detail into the list of the result. But I want all the employees either there is matching record or not.

How do I do it that whenever there is no matching record for work_location_id, it should just display "Not Specified" for work location instead of omitting the employee record?

Thanks.

0 likes
3 replies
tovisbratsburg's avatar

Are you able to do the query outside of Eloquent in something like MySQL on Workbench to get it to work how you want? My mind for something this complex gravitates to stitching it together in mysql and looking at it that way.

tovisbratsburg's avatar

Do you have the migration schemas-tables that you could paste and what specifically you want to do? That way I can see what the tables look like you are trying to work with.

Please or to participate in this conversation.