Ste77
Ste77

Reputation: 676

Laravel Left join returning empty result when record not available in right table

I'm having trouble with Laravel syntax. I have a leftjoin as such:

$query=DB::table('jobs');
$query->leftjoin( 'attribute_int_data', 'attribute_int_data.job_id', '=', 'jobs.job_id' );

The problem is that where there is no record in the attribute_int_data table, I do not get the job_id from the jobs table.

I understand this may be because I have two column names that are the same, and I need to apply an alias to one of the column names to get the job_id.

Can anyone help with the syntax please?

Cheers.

Upvotes: 1

Views: 1024

Answers (1)

Ste77
Ste77

Reputation: 676

Found the solution, hope it's of use to someone.

I added an array to the get, where I renamed the conflicting job_id column name.

$get_array = array( 
    'jobs.job_id as real_id',
    ...other values...
)

Then, to implement this, when I get the query:

$query->get($get_array);

And i get the job_id even when there are no matching records in the right table.

Upvotes: 1

Related Questions