2016-10-21 50 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)。我即將嘗試第二個選項 –