我在从我的日程安排表中调用离开国际民航组织和抵达国际民航组织的问题。 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)。我即将尝试第二个选项 –