【问题标题】:Laravel 5 hasManyThrough errorLaravel 5 hasManyThrough 错误
【发布时间】:2016-02-11 15:23:03
【问题描述】:

我从这些雄辩的关系中学到了很多东西,但我的理解并不完全在那里。

我有三张桌子:

候选人 • 身份证
• 候选人号码
• 名字
• 姓
• 出生日期 • created_at
• updated_at

结果 • 身份证
• 证书编号
• 候选人ID
• 资格_id
• created_at
• 更新时间

资格 • 身份证
• 代码
• 标题
• created_at
• 更新时间

一个候选人有很多结果,一个资格有很多结果。一个结果属于一个资格和一个候选人。在 candidaes.show 页面上,我想使用 hasManyThrogh 关系显示与该候选人相关的资格。 这是我的模型。

候选人:

<?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');
    }
}

结果模型:

<?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');
   }

}

资格模型:

<?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');
   }
}

在我的候选控制器中:

public function show($id)
{
    $candidate = Candidate::with('qualification')->find($id);

    return view('candidates.show', compact('candidate'));
}

在我看来:

@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

但是它正在重新调整以下错误:

SQLSTATE[42S22]: 找不到列: 1054 'on 子句'中的未知列 'qualifications.result_id' (SQL: select qualifications.*, results.candidate_id from qualifications inner join @987654330 @ on results.id = qualifications.result_id 其中results.candidate_id 在(17))

谁能帮助我或指出正确的方向?我什至不确定我是否使用 hasManyThrough 做正确的事情。

【问题讨论】:

    标签: laravel-5 eloquent has-many-through


    【解决方案1】:

    如果一个结果有多个 Qualifications,则需要在 Qualification Table 上放置一个外键。像这样:

    $table->integer('result_id')->unsigned();
    $table->foreign('result_id')->references('id')->on('results');
    

    您不需要删除表来添加它。只需转到您的终端并创建一个新的迁移文件

    php artisan make:migration edit_qualification_table --table=qualifications
    

    然后在 up 函数中添加这些。与 down 相同,因此如果您删除表,这些表也会被删除。

    【讨论】:

    • 对不起,我不确定我是否解释得很好。一个结果只有一个资格。至少这就是我想要做的。
    猜你喜欢
    • 1970-01-01
    • 2015-05-29
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-19
    • 2021-12-27
    • 1970-01-01
    相关资源
    最近更新 更多