同一张表中两列的杰出多个外键

4

我在调用我的航班计划表中的出发ICAO和到达ICAO时遇到了问题。Laravel一直报错,说我的关系有问题。以下是代码。

Schema::create('airports', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('city');
        $table->string('country');
        $table->string('iata');
        $table->string('icao');
        $table->double('lat');
        $table->double('lon');
        $table->longText('data')->nullable(); //JSON Data for All gate information for the system.
        $table->softDeletes();
    });
Schema::create('schedule_templates', function (Blueprint $table) {
        $table->increments('id');
        $table->string('code');
        $table->string('flightnum');
        $table->integer('depicao')->unsigned();
        $table->foreign('depicao')->references('id')->on('airports')->onDelete('cascade');
        $table->integer('arricao')->unsigned();
        $table->foreign('arricao')->references('id')->on('airports')->onDelete('cascade');
        $table->string('aircraft')->nullable();
        $table->boolean('seasonal');
        $table->date('startdate');
        $table->date('enddate');
        $table->time('deptime');
        $table->time('arrtime');
        $table->integer('type');
        $table->boolean('enabled');
        $table->text('defaults');
        $table->timestamps();
        $table->softDeletes();
    });

以下是模型列表

class ScheduleTemplate extends Model
{
    public $table = "schedule_templates";

    public function depicao()
    {
        return $this->hasOne('App\Models\Airport', 'depicao');
    }
    public function arricao()
    {
        return $this->hasOne('App\Models\Airport', 'arricao');
    }
}

class Airport extends Model
{
    //
    public $timestamps = false;

    public function schedules()
    {
        return $this->belongsToMany('App\ScheduleTemplate');
    }
}

当我尝试使用以下代码进行查询时,出现以下错误

SQLSTATE[42S22]: 列不存在: 1054 Unknown column 'vaos_airports.depicao' in 'where clause' (SQL: select * from vaos_airports where vaos_airports.depicao in (1))

$schedules = ScheduleTemplate::with('depicao')->with('arricao')->get();

最终目标是将结果放入表格中。如果您感兴趣,这是相关代码。
@foreach($schedules as $s)
     <tr>
          <td>{{$s->code}}</td>
          <td>{{$s->flightnum}}</td>
          <td>{{$s->depicao()->name}}</td>
          <td>{{$s->arricao()->name}}</td>
          <td>{{$s->aircraft}}</td>
          <td>{{$s->seasonal}}</td>
          <td>{{$s->type}}</td>
     </tr>
@endforeach

编辑:

我已经解决了关系问题,显然我把它们弄反了。这是更新后的模型类。

class ScheduleTemplate extends Model
{
    public $table = "schedule_templates";

    public function depicao()
    {
        return $this->belongsTo('App\Models\Airport', 'depicao');
    }
    public function arricao()
    {
        return $this->belongsTo('App\Models\Airport', 'arricao');
    }
}

class Airport extends Model
{
    //
    public $timestamps = false;

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

现在出错的地方在于视图文件。我会遇到BelongsTo错误:

未定义属性:Illuminate\Database\Eloquent\Relations\BelongsTo::$name

或者如果我没有使用“()”就有arricao或depicao:

试图获取非对象属性

1个回答

9
重点是,关系的第二个参数应该是外键,第二个参数是本地键。
从文档中可以看到:
return $this->hasOne('App\Phone', 'foreign_key', 'local_key');

在您的情况下,请尝试以下操作:
public function depicao()
{
    return $this->hasOne('App\Models\Airport', 'id', 'depicao');
}
public function arricao()
{
    return $this->hasOne('App\Models\Airport', 'id', 'arricao');
}
更新 这些错误是由于您在此关系中使用了相同的列名。在我看来,有两种解决方案:
  1. 尝试从关系中获取第一个对象,像这样。 但请注意:贪婪加载将不起作用!

    <td>{{$s->depicao()->first()->name}}</td> <td>{{$s->arricao()->first()->name}}</td>

  2. 重命名您的关系或列,以便它们不重叠当前列名。这是目前最好的选择。

例如,您可以将列更改为depeciao_idarricao_id,这也表明这些列引用了具有相应ID的另一个表格,这更加描述性。

尝试了上述方法以及我的修改。使用您的解决方案出现此错误。 - Taylor Broad
未定义属性:Illuminate\Database\Eloquent\Relations\HasOne::$name - Taylor Broad
检查了您的更新。它没有返回错误,但是它返回了相同条目的两倍。我将波特兰国际机场作为出发地和到达地(这意味着它从数据库中获取了第一条记录而没有检查ID)。我即将尝试第二个选项。 - Taylor Broad
如果你有多个外键,这将非常有帮助。 - Dushyant Joshi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接