简体   繁体   中英

create a PHP multidimensional array in laravel 5.4 for the mysql join statement which which return multiple row for a user id

I have a jobs table, which has a job id and some other stuff

jobs

id job_title job_profile
1 software engg java developer

and another table could be job location table

job_location

id city job_id
1 Delhi 1
2 Mumbai 1

Here job_location.job_id is the jobs.id

Now I want to fetch a job consisting of all the possible city.

I tried

$job_ids = DB::table('jobs');
$job_ids->where ('jobs.id', $id);
$job_ids->join('job_location', 'jobs.id', '=', 'job_location.job_id');

$job_ids = $job_ids->get();

Now its giving me two object of jobs, one for each cities. Output:

[{"id":1,"job_title":"Job Title Software Developer","job_profile":"Associate Software Developer","city":"Delhi"},{"id":1,"job_title":"Job Title Software Developer","job_profile":"Associate Software Developer","city":"Mumbai"}]

Expected Result:

[{"id":1,"job_title":"Job Title Software Developer","job_profile":"Associate Software Developer","city":{"Delhi","Mumbai"}}]

Any help would be appreciated. Thanks

Note: I am using laravel query builder

That's how an SQL join works for a one to many relationship: it will return a joined row for your jobs table on top of every row it matches with. It's probably easiest to just have a separate search where you search for all job_locations that have job ids that exist in the jobs table and then combine the results

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM