【问题标题】:Eloquent Multiple Foreign Keys from same table in two columns两列中来自同一张表的雄辩的多个外键
【发布时间】:2017-03-03 07:19:59
【问题描述】:

我在从我的日程表中呼叫出发国际民航组织和到达国际民航组织时遇到问题。 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 'where 子句'中的未知列'vaos_airports.depicao'(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

试图获取非对象的属性

【问题讨论】:

    标签: php mysql laravel eloquent


    【解决方案1】:

    关键是,关系的第二个参数应该是外键,第二个是本地键。

    来自文档:

    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的另一个表,这样更具描述性。

    【讨论】:

    • 尝试了上述以及我的编辑。使用您的解决方案出现此错误
    • > 未定义属性:Illuminate\Database\Eloquent\Relations\HasOne::$name
    • 检查了您的更新。它返回没有错误,但它返回相同条目的两倍。我将波特兰国际作为出发地和到达地(这意味着它在不检查 id 的情况下从数据库中提取第一条记录)。我将尝试第二个选项
    • 如果你有多个外键会很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-18
    • 2019-05-01
    • 2020-08-20
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多