I messed up the formatting for this post again sorry about that. I can’t seem to edit it for some reason :(
hasManyThrough error
I’m learning a lot about these eloquent relationships but I’m not quite there in my understanding.
I have three tables:
Candidates
• id
• candidate_number
• givennames
• familyname
• dob
• created_at
• updated_at
Results
• id
• certificate_number
• candidate_id
• qualification_id
• created_at
• updated_at
Qualifications
• id
• code
• title
• created_at
• updated_at
A candidate has many results and a qualification has many results. A result belongs to a qualification and a candidate. On the candidaes.show page I want to show what qualifications are related to that candidate using a hasManyThrogh relationship.
Here are my models.
Candidates:
use Illuminate\Database\Eloquent\Model;
class Candidate extends Model {
protected $table = 'candidates';
public $timestamps = true;
public function result()
{
return $this->hasMany('App\Result');
}
public function qualification()
{
return $this->hasManyThrough('App\Qualification', 'App\Result');
}
}```
Result Model:
```<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Result extends Model {
protected $table = 'results';
public $timestamps = true;
public function candidate()
{
return $this->belongsTo('App\Candidate');
}
public function qualification()
{
return $this->belongsTo('App\Qualification');
}
}```
Qualification Model:
```<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Qualification extends Model {
protected $table = 'qualifications';
public $timestamps = true;
public function result()
{
return $this->hasMany('App\Result');
}
public function candidate()
{
return $this->hasManyThrough('App\Candidate', 'App\Result');
}
}```
In my candidate controller:
```public function show($id)
{
$candidate = Candidate::with('qualification')->find($id);
return view('candidates.show', compact('candidate'));
}
and in my view:
@extends('app')
@section('content')
<h1>{{ $candidate->givennames }} {{ $candidate->familyname }}</h1>
<div class="body"> {{ $candidate->dob }} </div>
<div class="body"> {{ $candidate->candidate_number }} </div>
<h3> Qualifications </h3>
@foreach($candidate->qualification as $qualification)
<div class="body"> {{ $qualification->title }} </div>
@endforeach
@stop```
However it is retuning the following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'qualifications.result_id' in 'on clause' (SQL: select `qualifications`.*, `results`.`candidate_id` from `qualifications` inner join `results` on `results`.`id` = `qualifications`.`result_id` where `results`.`candidate_id` in (17))
Can anyone help me or point me in the right direction? I’m not even sure I am doing the right thing with a hasManyThrough.
Please or to participate in this conversation.