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

king_eke's avatar

How to Query two tables in one query statement

i have a table that contains all job offers.. but some have gotten approved by the client.. so i want all those that are approved to not show.. i'm using relations.. i have two tables.. one is where the jobs are (jobs) and the other (applied_jobs) are the ones that someone has applied too and it contains a status column which tells me the status of the job (status) so now assuming a job from (jobs) gets approved in (applied_jobs) how can i make a query that would show all the jobs even those in the (applied_jobs) that don't have their status as true which. i hope you understand

0 likes
21 replies
JackJones's avatar

You need to show your columns with some dummy data and explain what you need

king_eke's avatar

on the jobs table

id |    client_id | job_title | job_desc
1        1         first job    blah blah
2        2             another  blah blah   
3        1         second   blah blah

on the applied job

id | developer_id | client_id | job_id | status
1       1               1        1            1

so i have something like this..

I want to show all the data on the jobs table where the status on the applied job isn't true(1)...

Cronix's avatar

Assuming you have a hasMany relationship called appliedJob on the Job model...

$jobs = Job::with('appliedJob', function($query) {
    $query->where('status', '!=', 1);
})->get();
king_eke's avatar

I get this error mb_strpos() expects parameter 1 to be string, object given

Cronix's avatar

You'll need to show more code then besides just your schema. Like do you have a appliedJob relationship on the Job model? Show your Job model and AppliedJob model.

king_eke's avatar

on the job model

public function job_applied(){
        return $this->hasMany('App\AppliedJob');
    }

on the appliedjob model

 public function job(){
        return $this->belongsTo('App\Job');
    }

Cronix's avatar

Did you adjust the relationship name in my query to match yours? I used appliedJob, but your actual relationship name is job_applied

Snapey's avatar

Using with gives all jobs and joins applied jobs where status is 1

If you only want jobs where applied status is not 1

$jobs = Job::whereHas('job_applied', function($query) {
            $query->where('status', '!=', 1);
        })
        ->with('job_applied')
        ->get();

king_eke's avatar

i get this error @Snapey Call to undefined method Illuminate\Database\Query\Builder::job_applied()

Snapey's avatar

try

$jobs = Job::whereHas('jobApplied', function($query) {
            $query->where('status', '!=', 1);
        })
        ->with('jobApplied')
        ->get();
king_eke's avatar

Call to undefined method Illuminate\Database\Query\Builder::jobApplied()

@Snapey

Snapey's avatar

Do your relationships work in other places?

Snapey's avatar

so

php artisan tinker;

App\Job::with('job_applied')->first()

works ok?

king_eke's avatar

it's working now... i had {{$jobs->links()}} in the view and the code didn't have paginate on it..

but now its bringing the jobs from the approved_jobs table only.. and not the whole jobs

what i'm trying to do is bring out the whole jobs which would include the ones in the applied_jobs table.. but then it would exclude the ones in the applied_jobs table where the status there is true.. therefore bringing out all the jobs that haven't been assigned true.. the job table doesn't have a status column..

king_eke's avatar

i hope you understand what i'm trying to do

like assuming on the jobs table there are

1 - job 1
2 - job 2
3 - job 3
4 - job 4

then you apply for job 3 and job 4

so in the applied_jobs table you'd see

1 - job 3 
2 - job 4

then the person approves you for job 3

so now job 3 has a status of true

so i would want to get the results of all the jobs without that job 3, since it has been approved

so i'd get

job 1
job 2
job 4

hope i'm clear ?

Snapey's avatar

What code are you using, mine or cronix

Please show your current code

king_eke's avatar

yours

 $jobs = Job::whereHas('job_applied', function($query) {
            $query->where('status', '!=', '1');
        })->with('job_applied')
        ->get();

Snapey's avatar
Snapey
Best Answer
Level 122

So, I expect that this should return all jobs with an applied job and the status is not 1

You want all the jobs, not just those with applied_job.status=1

There is no check here that the jobs relate to an individual?

$jobs = Job::whereDoesntHave('job_applied', function ($query) {
    $query->where('status', '!=', 1);
})->get();

Please or to participate in this conversation.