Search code examples
laravelinner-joinlaravel-5.3laravel-query-builder

Laravel 5.3 inner join not working properly


I'm having two tables as 'jobs' and 'desired_skills'. Table structure is as follows.

jobs table jobs Table

desired_skills table desired_skils table

where desired_skills.job_id refers to jobs.job id

In controller I have (I am getting $id as an argument from the url, and I can confirm the argument grabs the desired value)

$jobs = DB::table('jobs')->where(function ($query) use ($id) {
    $query->Join('desired_skills', 'desired_skills.job_id', '=', 'jobs.job_id')
    ->where('jobs.employer_id', '=', $id);
    ->select('*')
})->get();

when I dump and die $jobs it only returns values from jobs table. but when I run the query

SELECT * FROM jobs INNER JOIN desired_skills ON desired_skills.job_id = jobs.job_id it returns the desired value set.

What am I doing wrong? Any help will be greatly appreciated.


Solution

  • I think it has to do with wrapping your join inside of a where clause. I don't think it's giving you your desired query with that there.

    $jobs = DB::table('jobs')
        ->join('desired_skills', 'desired_skills.job_id', '=', 'jobs.job_id')
        ->where('jobs.employer_id', '=', $id)
        ->get();