Reputation: 519
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:
<?php namespace App;
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> Centre </h3>
<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.
Upvotes: 0
Views: 352
Reputation:
If a result has many Qualifications then a foreign key needs to be placed on the Qualification Table. Like so:
$table->integer('result_id')->unsigned();
$table->foreign('result_id')->references('id')->on('results');
You don't need to delete the table to add this. Just go to your terminal and create a new migration file
php artisan make:migration edit_qualification_table --table=qualifications
Then add these within the up function. Same with down so if you do delete the table these will be deleted too.
Upvotes: 0