我在從我的日程安排表中調用離開國際民航組織和抵達國際民航組織的問題。 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
wherevaos_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
嘗試了以上以及我的編輯。使用您的解決方案獲取此錯誤 –
>未定義的屬性:Illuminate \ Database \ Eloquent \ Relations \ HasOne :: $ name –
檢查了您的更新。它沒有錯誤地返回,但它返回的是相同條目的兩倍。我有波特蘭國際作爲出發地和到達地(這意味着它將數據庫中的第一條記錄從數據庫中取出,而無需檢查該ID)。我即將嘗試第二個選項 –