私のスケジュール表から出発ICAOと到着ICAOを呼び出す際に問題があります。 Laravelは、私の関係が台無しになったというエラーを私に与え続けます。ここにコードがあります。ここで2つの列に同じテーブルの複数の異種キーがあります
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
EDIT:
私はどうやら、私は彼らが交換されていた関係の問題を修正しました。更新されたモデルクラスは次のとおりです
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のエラーを取得するか:
Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$name
か、この私がなしarricaoまたはdepicaoを持っている場合は、「()」
Trying to get property of non-object
上記と私の編集を試してみてください。 –
>未定義のプロパティ:Illuminate \ Database \ Eloquent \ Relations \ HasOne :: $ –
更新をチェックしました。エラーなしで返しますが、同じエントリの2倍を返しています。私は出発と到着の両方としてポートランド・インターナショナルを持っています(つまり、IDをチェックせずに両方のデータベースから最初のレコードを引っ張っています)。私は2番目のオプションを試してみようとしています –