2016-10-21 27 views
1

我在从我的日程安排表中调用离开国际民航组织和抵达国际民航组织的问题。 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]: Column not found: 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'); 
    } 
} 

错误现在存在于视图文件中。我将在得到一个属于关联错误:

Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$name

或者,如果我有arricao或depicao无“()”

Trying to get property of non-object

回答

2

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

从文档:

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,这是更具描述性的。

+0

尝试了以上以及我的编辑。使用您的解决方案获取此错误 –

+0

>未定义的属性:Illuminate \ Database \ Eloquent \ Relations \ HasOne :: $ name –

+0

检查了您的更新。它没有错误地返回,但它返回的是相同条目的两倍。我有波特兰国际作为出发地和到达地(这意味着它将数据库中的第一条记录从数据库中取出,而无需检查该ID)。我即将尝试第二个选项 –