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

princeparaste's avatar

Need a Laravel Left Join query to find out wheather the Job is assigned or not to user (with some conditions))

Table 1 Name: job_table job_table https://imgur.com/TNQBICS

Table 2 Name: assign_Table assign_table https://imgur.com/ecaCd3P

I want a laravel query where i want to left join job_table with assign_table and

CASE 1 : if the job is assigned in the assign_table and have user_id and assign_date not null then i want the result to show job_code, user_id and assign_date

Otherwise CASE 2: if the job_code not exist in the assign_tableor the user_id OR THE assign_date is null then i want the result to show job_code user_id and assign_date (in this case user_id and assign_date will show as null in result)

Note : There can be multiple same job code exist in the assign_Table.

My query is something like this but its not no where near what i want:

DB::table('job_table')
                ->select('job_table.job_code','assign_table.assign_date','assign_table.user_id' )
                ->leftJoin('assign_table','assign_table.job_code','=','job_table.job_code')
                ->where(function($query) {
                   $query->whereNotNull('assign_table.tech_id')
                        ->whereNotNull('assign_table.assign_date');
                        ->groupBy('assign_table.workorder_id');
                })
                ->get();
0 likes
1 reply
SilenceBringer's avatar

@princeparaste sometimes it's much more easy to understand the problem when you provide ACTUAL reasult and EXPECTED result. For now you query looks ok for your description

1 like

Please or to participate in this conversation.