2

I have 2 tables called jobs & job_records. Its relationship as below:

JobRecords Model

public function job()
{
   return $this->belongsTo(Job::class);
}

Job Model:

public function jobRecord()
{
   return $this->hasOne(JobRecord::class);
}

jobs table has 2 columns that I need to display alongside my job_records table view. It's total_pges & status.

In my JobRecords Controller, I have tried the following method. It throws me an error of Call to undefined relationship.

JobRecordController:

$job_records = JobRecord::whereStatus('In Progress')
  ->with('jobs', 'jobs.status', 'jobs.total_pges')
  ->get();
return DataTables::of($job_records)

I am still beginning with Laravel and PHP. I can sense that there is something wrong with the relationship. But I couldn't figure out what it is exactly. Can anyone help me out with this matter?

3
  • 1
    Your relationships are upside down. Job model needs to have relationship with job record not itself and the job record model needs relationship with job not itself. Commented Oct 5, 2019 at 7:35
  • Yes. I have seen this after posting the question. I have revised my question. Extremely sorry for the mistake. It already exists as you are suggesting. Commented Oct 5, 2019 at 7:45
  • with is for eager loading of relationship. it seems you are trying to join two tables using with which is not correct. your relationship is job and you are using jobs thus the application is throwing the undefined relationship error. Commented Oct 5, 2019 at 8:13

2 Answers 2

1

In your JobRecord model change the relation ship as

 public function job()
 {
  return $this->hasOne('App\Models\Job','foreign_key','local_key');
 }

Similarly, in Job model

 public function job()
 {
  return $this->belongsTo('App\Models\JobRecord','foreign_key','local_key');
 }

Replace foreign_key and local_key with appropriate values...

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much @jithesh jose. Your relationship suggestion was spot on. Thank you very much.
0

I deleted my previous answer. What are you trying to do exactly? You can't use "jobs" in the "with function" without to define "jobs" as function in the model.

If you change it to "job" (instead of "jobs), then it would work, but I don't know if you want this. With your query you saying that a record have many jobs? But your model doesn't define that.

6 Comments

I just want to get these two columns from the jobs table. In this function, I used jobs because it's the table name.
$job_records = JobRecord::whereStatus('In Progress') ->with('jobs', 'jobs.job_id', '=', 'job_records.job_id') ->select('jobs.total_pges', 'jobs.status') ->get(); return DataTables::of($job_records) Is this way correct?
$job_records = JobRecord::whereStatus('In Progress') ->with('job:id,status,total_pges') ->get(); should do it.
I have tried this with jitesh jose relationship suggestion and it worked. Thank you very much @arkert.
You don't need to set foreign_key specifically, if you use documented naming conventions. See here: laravel.com/docs/master/eloquent-relationships . And remember: in the "with function" you don't use the name in the database, but the name of the function in the model. So if you use "->with('job')" then there has to be a function job() in the model. :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.