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

princeparaste's avatar

Need help in laravel eloquent query to get all the job_id with additional status.

Table: job_table
----------------------------------------------------------------------------
	id		| 	jobcode |	updated_at	| created_at
----------------------------------------------------------------------------
	1		|	x123	   | 2020/11/03 	|2020/11/03
	2		|	x124	   | 2020/11/03 	|2020/11/03
	3		|	x125	   | 2020/11/03 	|2020/11/03
	4		|	x126	   | 2020/11/03 	|2020/11/03
	5		|	x127	   | 2020/11/03 	|2020/11/03

Table: assign_table

-----------------------------------------------------------------------
	id		|	jobid 	| 	assign_user_id | assign_date | 	updated_at	| created_at
--------------------------------------------------------------------------
	1		|	2	    |	null	        |  null	     | 2020/11/03 	|2020/11/03
	2		|	2	    |	1241	        | null	     | 2020/11/03 	|2020/11/03
	3		|	3		|	1251	        | 2020/11/03 | 2020/11/03 	|2020/11/03
	4		|	3		|	1261	        | 2020/11/03 | 2020/11/03 	|2020/11/03
	5		|	2		|	1271	        | 2020/11/03 | 2020/11/03 	|2020/11/03

Resultant Table : 

------------------------------------------------------------------------------------------------------
	id		| 	jobcode |	assign_status    |	updated_at	| created_at
------------------------------------------------------------------------------------------------------
	1		|	x123	   |   not_assigned	| 2020/11/03 	|2020/11/03
	2		|	x124	   |   assigned		| 2020/11/03 	|2020/11/03
	3		|	x125	   |   assigned		| 2020/11/03 	|2020/11/03
	4		|	x126	   |   not_assigned	| 2020/11/03 	|2020/11/03
	5		|	x127	   |   not_assigned	| 2020/11/03 	|2020/11/03
	

I have 2 tables : job_table, assign_table . I want to get resultant query to display like resultant table as shown above.

Note: I want to get the resultant query to show all the jobid present in the job_table but also add assign_status in the resultant query show that i can show that assign_status in the frontend table to user weather that job is assigned or not.

assign_status = assigned will be shown only if jobid is present in assign_table and have not null values in both field assign_user_id and assign_date. otherwise it will show assign_status as not_assigned

Is this possible to get this type of result or may be there is some other way to procced to get the result ?

0 likes
1 reply
Snapey's avatar

easy with relationships, but you have made life a lot harder by not using conventions for table and column names

Do you have models created for job and user?

Please or to participate in this conversation.