Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel – Eloquent join query not working

I’m doing a simple join query of job_roles and operations table.
The fk is the column job_role_id of operations references to the column id of job_roles.

Output that I need is get the total count of data from operations where operations.structure_cost is not equal to job_roles.cost_type

I have a working query created in MySQL Workbench using this code below:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

SELECT COUNT(*) as total
   FROM
job_roles t1
   RIGHT JOIN
operations t2 ON t2.job_role_id = t1.id
   WHERE
t2.structure_cost != t1.cost_type
t2.master_id = 1; ### get operations only for auth user

Below is my query in Laravel that don’t work. Count that I’m getting in below data example is 3 which is wrong, Count should return 1 as what is indicated in the where clause

enter image description here

table name: job_roles

enter image description here

table name: operations

enter image description here

>Solution :

Third parameter of where function is used as a value and not as a column name. Use whereRaw function

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading